Question

What are your favorite command line tips or tricks?

Posted September 16, 2016 15.8k views
Linux BasicsLinux Commands

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 entered
  • sudo !! - Re-runs the last command you entered as a super user

What are yours? (No cheating.)

edited by etel
3 comments

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.

×
Submit an Answer
23 answers

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]

  • I use screen on quite a few projects to quickly get a process running in the background while allowing me to switch back to it as needed whether it’s a minecraft server or a ruby script. Your reply is a great quick reference for commonly used screen commands. We also have this tutorial which walks through using screen step by step.

    by Justin Ellingwood
    In this article, we will discuss how to install and use "screen", a tool that allows you to manage multiple shell sessions inside a single terminal. The screen program is very useful for multi-tasking in an ssh environment, due to its ability to resume terminal sessions and perform multiple-concurrent tasks.

Ctrl+r i.e. reverse search is my favorite.

I 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
  • rsync for moving stuff
  • wget is great (especially with the -c (continue) flag)
  • gnu parallel for running batch jobs in…parallel
  • flock for making sure your cron job is only running one instance at any given time
  • the $() for getting the output from a command that was mentioned earlier is great, and you can also
  • I’m not sure where exactly it comes from, I think it ships with perl in deb-based distros, but the rename command is great for bulk-renaming jobs

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

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)

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

Which :)

which name, for instance where is the dnf command on fedora..
which dnf gives /usr/bin/dnf

last 

It is pretty handy when you need to know if were a failure or to audit the system login, restart, or shutdown.

maybe not that much, but i love tail -f to see what’s happening live on some demon’s logs

Previous 1 2 3 Next