In the same vein as my previous question about bash aliases, what are your favorite command line tricks? What command has saved you the most time? What bash features did you never realize existed and now use daily and can’t live without?
Of course, there are the classics:
!!
- Re-runs the last command you enteredsudo !!
- Re-runs the last command you entered as a super userWhat are yours? (No cheating.)
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
screen
is one of my favorites. You can do so many things with it. It’s really handy when you want something to keep running after you disconnect from SSH. To disconnect a running screen
press ctrl + a, ctrl + d
. To see your screens screen -list
. To restore one screen -r [somescreenname]
Ctrl+r i.e. reverse search is my favorite.
rsync
for moving stuffwget
is great (especially with the -c
(continue) flag)flock
for making sure your cron job is only running one instance at any given time$()
for getting the output from a command that was mentioned earlier is great, and you can alsorename
command is great for bulk-renaming jobsI love pushd
and popd
for treating your paths as an array. Good for backtracking and for scripting. Here’s an example:
$ mkdir -p my/test/folder
$ pushd my/test
~/my/test ~
$ pushd folder
~/my/test/folder ~/my/test ~
$ popd
~/my/test ~
$ pwd
/Users/brianhogan/my/test
$ popd
~
$ pwd
/Users/brianhogan
Wrapping subcommands in $() is pretty brilliant for chaining things together.
For example, you can put together grep and awk to find a docker container via a docker-compose name to execute a script on. A silly example, but it works.
docker exec $(docker ps -aqf "name=$(docker-compose ps | awk '{print $1}'|grep "db")") bash "/run/a/script/import.sh"
Here is a simpler example that stops all docker containers:
docker stop $(docker ps -q)
Some GNU readline hotkeys:
Ctrl + W - delete previous argument and add to yank ring Ctrl + Y - paste last item from yank ring Ctrl + U - delete all the symbols left from the cursor Ctrl + K - delete all the symbols right from the cursor
Also, The hash which keeps track of the number of times you’ve called a given outer command within the current shell:
[cvetomir@localhost:~]$ hash hits command 1 /usr/bin/pwgen 1 /usr/bin/vim 2 /bin/ls
Which :)
which name, for instance where is the dnf command on fedora… which dnf gives /usr/bin/dnf
rsync I love rsync… I rsync all over the place…backups…moving websites to another folder…you can even rsync to another server with ssh:
rsync -e -av --recursive --progress --rsh='ssh -p3222 user@domain.com:folder/ ~/folder
last
It is pretty handy when you need to know if were a failure or to audit the system login, restart, or shutdown.
https://mosh.org/ instead of SSH! http://ohmyz.sh/ with several plugins.
Click below to sign up and get $100 of credit to try our products over 60 days!
Just typed
calendar
in Ubuntu, it brought a list of major events in history that happened today. Good general knowledge.You can even use sed (I think) to alter the command:
!!:s/foo/bar
@m1025 Forgot about
calendar
! Nice shout out.