Linux tricks to speed up your workday

Sometimes a new way of getting work done on the Linux command line can be both interesting and time-saving

Network World - Insider asset - Invaluable Tips + Tricks for Troubleshooting Linux [Winter 2018]
Network World / IDG

One of the really nice things about working on the Linux command line is that you can get a lot of work done very quickly. With a handle on the most useful commands and some command-line savvy, you can take a lot of the tedium out of your daily work. This post will walk you through several handy tricks that can make your work load feel a little lighter and maybe be a little bit more enjoyable.

Emptying files with >

Any time you have an important file that you need to empty because it's become too large or the data is no longer needed, you can do that by using the command > filename. This is much faster than removing the file and recreating it with the original permissions. The > sign followed by the file name works the same as typing cat /dev/null > filename, but is wonderfully quick. It empties the file, but leaves permissions and ownership intact.

$ ls -l bigfile
-rw-r--r--. 1 shs shs 8956108 Mar 23 10:00 bigfile
$ > bigfile
$ ls -l bigfile
-rw-r--r--. 1 shs shs 0 Mar 23 10:50 bigfile 

Nothing changes about the file but the size and the last updated time.

It's quite common to use the > operation to empty a log file that has become too large, especially if it's one that is not rotated by the system.

Saving edits to a file owned by root

If you sometimes need to edit a system file, but forget to start your text editor (vi or vim) with sudo only to find that you then can't save your changes, you don't have to start over from scratch! Instead, you can press the : key as you would when getting ready to save the file, but then type this:

w !sudo tee %

You'll be prompted to enter your password (as with any sudo command) and be asked to confirm your intention of overwriting the file, but you won't have to redo all the changes you have made. After the file is saved, press the enter key, exit with :q! (changes already made), and you're done.

One command to copy a file to multiple locations

Commands like the one shown below make it possible for you to run a command a number of times without retyping it. In this case, the echo command sends the list of file system locations to xargs which then runs the cp command for each of them. The -v argument coaxes cp into displaying what it's doing. Replace the location list (i.e., loc1 ...) with your target directories.

$ echo loc1 loc2 loc3 | xargs -n 1 cp -v myfile

The commands that would run in this example would be:

cp -v myfile loc1
cp -v myfile loc2
cp -v myfile loc3

The "loc" arguments should be replaced with file system paths (e.g., /usr/local). If the target directories are not ones that you can write to without using sudo, use a command like this instead:

$ echo loc1 loc2 loc3 | sudo xargs -n 1 cp -v myfile

Here's an example using root access to copy a file into a number of home directories:

$ echo /home/nemo /home/lola /home/shark | sudo xargs -n 1 cp -pv tasks
'tasks' -> '/home/nemo/tasks'
'tasks' -> '/home/lola/tasks'
'tasks' -> '/home/shark/tasks'

This command uses sudo to copy the files into the target directories, but this is not required for listing the directories and passing them to the xargs command.

Note that it would also be very easy to turn a command like this into an alias since only the final argument--the file name--would need to change as you use it. Here's an example of such an alias:

$ alias cp2dirs="echo /home/nemo /home/lola /home/shark | sudo xargs -n 1 cp -pv"

To use this alias, you would only need to supply the file to be copied and provide your password when asked for the sudo portion of the alias.

$ cp2dirs newfile
[sudo] password for you:

Using a similar strategy,  you could create an alias that copied a file into every home directory on your system. However, the files would retain their permissions, owner, and group assignments unless you followed up with chown or chmod operations. Here's an example alias:

alias cp2all='find /home -maxdepth 1 -type d | tail -n +2 | grep -v lost+found | sudo xargs -n 1 cp -pv'

This cp2all alias works by creating a list of home directories (avoiding /home itself and the lost+found directory in case /home is a separate file system) and then copying the file specified into each of them. It could end up overwriting existing files by the same name, so you should be careful.

For more coverage of using the xargs command, check out this earlier post on xargs.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Related:

Copyright © 2021 IDG Communications, Inc.

IT Salary Survey 2021: The results are in