The bash history can do many helpful things, and the search with Strg-r
that Terry Wang mentioned is an important one among them. But it is also possible to do exactly what you asked for.
You can re-do the previous command with !!
. With this, you can also edit the previous command. If for example you forgot to get root privileges for a command
apt-get install a-long-list-of-packages
you don't have to retype all of that again. Instead just call
sudo !!
If you want to re-execute the command at a specific position from your history, you can also use !
, for example
!3
to re-execute the command at position 3. Be aware that this counts from the top. So if you're storing 500 commands in your history, !1
would be "500 commands ago". You can also use negative numbers. For example
!-2
would re-execute the second last command.
You can also re-execute the last command that started with a string like
!apt-
which would re-do the last line that started with "apt-". If you want the last command where the string appeared anywhere in the line, you can use something like
!?pt-ge
There are more interesting things the bash history can do. Just to give an impression of the wide range of possibilities, you can specifically access a parameter of a command from history. So
!-5:3:p
would print the third parameter to the fifth from last command.
EDIT: Regarding Rudie's comment below, with the standard settings this bash history expansions are indeed executed directly. It's probably best described like this: A call like !-3
is replaced by the shell with the third last command from your history and then your input (with the replacement) executed. So if you type !-3
and press ENTER
and your third last command was ls ~
, it's in effect the same as if you typed ls ~
again and pressed ENTER
"on your own".
If you don't want that, you can set the shell option histverify
. For setting and unsetting shell options, you might want to read up on the shopt
command. With histverify
set, a call like !-3
only writes the replacement from your history to your command line, but doesn't execute it directly. You have, so to speek, press the crucial ENTER
yourself - or refrain from it, if you choose to.
I assume you mean you have a structure something like:
├── f1
│ ├── a1
│ ├── a2
│ ├── b1
│ ├── b2
│ ├── c1
│ ├── c2
├── f2
│ ├── a3
│ ├── a4
│ ├── b3
│ ├── b4
│ ├── c3
│ ├── c4
and you want to end up with a directory like this:
a-files
├── a1
├── a2
├── a3
└── a4
Assuming:
- the current working directory is the parent directory of all the directories
f1 f2 f3
You could do:
mkdir a-files
for files in f*/a* ; do cp "$files" a-files ; done
to copy all files starting with a
to a new directory a-files
from all directories starting with f
. You can repeat for files starting with b...
mkdir b-files
for files in f*/b* ; do cp "$files" b-files ; done
Note: if there are any duplicate filenames, each file written to the new directory will overwrite another with the same name, so at the end of the loop, the new directory would only have a copy of the last file to be written with that name. You could use the -n
flag to cp
to prevent overwriting, and then you would get the first file with that name instead of the last one:
for files in f*/a* ; do cp -n "$files" a-files ; done
Best Answer
bash
offers the Ctrl+Alt+e shortcut (from thebash
man page):Examples:
ls
Ctrl+Alt+e!268
Ctrl+Alt+e$PATH
Ctrl+Alt+e