Linux tricks to speed up your workday

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.

Copyright © 2021 IDG Communications, Inc.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker