Using command options and arguments to get just the right output on Linux


#command #options #arguments #output #Linux

This post covers some well-known Linux commands that, when used with particular options and arguments, can save you some time or ensure that what you are doing is what you intended. The first “trick” involves how you exit vi or vim and the difference that can make.

Using 😡 instead of :wq when saving files with vi or vim

The vi and vim editors are the most commonly used text editors on Linux systems. When you exit either with 😡 instead of the more common :wq, the file will only be saved if you have just made changes to it. This can be helpful if you want to ensure that the file’s timestamp reflects its most recent changes. Just keep in mind that, if you make a change to a file and then undo those changes – like deleting a word or a line and then replacing it with the same content, this will still be seen as a change and vi or vim will save the file, updating the timestamp whether you use 😡 or :wq.

Ask the mv command to explain what it’s doing

The mv command can be used to rename a file or just move it to an alternate location – depending on whether the second argument that you provide is a directory or simply a file name. The following command moves the file shown to the dir2 directory. If there is no such directory, it would rename the file “dir2”. Using the -v argument allows you to see exactly what is happening, making it easy for you to spot the problem if this isn’t what you intended.

$ mv -v dumfile dir2
renamed 'dumfile' -> 'dir2/dumfile'

While the explanation above says “renamed”, it’s clear from the output that the file is being moved into a different directory. That output following the mv command will only appear if you use the -v option.

If the second argument isn’t a directory, it is taken to be a file name and the file is simply renamed as in this example:

$ mv -v yo yo.txt
renamed 'yo' -> 'yo.txt'

As you can see, using the -v (verbose) option with mv means the command will explain what it has done. This output can confirm or refute that mv commands are doing what you intended.

Listing files by age or size

To list files by age, use the -ltr arguments with the ls command. Here’s an example that displays the five most recently updated files:

$ ls -ltr | tail -5
-rw-------. 1 shs shs    204 Oct  5 14:14 nohup.out
-rwx------. 1 shs shs     75 Oct  5 14:16 big-loop
-rw-r--r--. 1 shs shs      3 Oct  5 14:26 loop.txt
drwxr-xr-x. 1 shs shs    140 Oct  5 16:44 videos
drwxr-xr-x. 1 shs shs     34 Oct 11 16:27 dir2

The -l option, as you undoubtedly know, gets ls to provide a “long listing” that shows permissions, update times, etc. The -t (time) argument orders the files by update times. The -r (reverse) argument ensures that the most recently updated files are listed last.

To list files by size (largest last), use a command like this where “S” refers to the file size and “r” ensures that the largest files are listed last:

$ ls -lSrh | tail -5
-rw-r--r--. 1 shs shs  77K Sep 19 11:42 linux_tips
-rw-r--r--. 1 shs shs  79K Sep 19 11:48 tips.html
-rw-r--r--. 1 shs shs  79K Sep 19 11:40 linux_tips.html
-rw-r--r--. 1 shs shs 125K Sep 19 11:30 linux.html
-rw-r--r--. 1 shs shs 211K Sep 16 14:30 geeks

Listing directories by size

The command shown below will use the du command to get file sizes and the -h option to display them in the human-friendly format. The -h option used with the sort command ensures the files are sorted by size.

$ du -h | sort -h | tail -5
46M     ./.cache/mozilla
46M     ./.cache/mozilla/firefox
46M     ./.cache/mozilla/firefox/c8n9kgaz.default-release
89M     ./.cache
115M    .

Finding commands related to a particular search term

The -k option with the man command (sometimes referred to as the “apropos option”) will search for commands that are related to the search term. Here’s an example of its usage:

$ man -k password | head -5
chage (1)            - change user password expiry information
chgpasswd (8)        - update group passwords in batch mode
chpasswd (8)         - update passwords in batch mode
cracklib-check (8)   - Check passwords using libcrack2
endpwent (3)         - get password file entry

Another way to find commands related to a particular term is to simply use the apropos command. Notice that the output below is identical to what is shown above.

$ apropos password | head -5
chage (1)            - change user password expiry information
chgpasswd (8)        - update group passwords in batch mode
chpasswd (8)         - update passwords in batch mode
cracklib-check (8)   - Check passwords using libcrack2
endpwent (3)         - get password file entry

Listing files by a particular date

To list only those files created or modified today, use a command like this one:

$ ls -al --time-style=+%D | grep `date +%D`
drwx------. 1 shs  shs    1450 10/11/23 .
-rw-------. 1 shs  shs      59 10/11/23 .lesshst

The –time-style option changes the way the date is formatted. Compare the difference in these examples:

$ date
Wed Oct 11 01:26:11 PM EDT 2023
$ date +%D
10/11/23      <== format used in the command shown above

You can list a file for any date by specifying the date in a command like this:

$ ls -al --time-style=+%D | grep '10/05/23'
-rwx------. 1 shs  shs      75 10/05/23 big-loop
-rwx------. 1 shs  shs      74 10/05/23 long-loop


Sometimes simple changes to the commands that you run can make your day move more smoothly. I hope that at least one of the tips described in this post does that for you. Don’t forget to turn commands into aliases to make them easier to use. Here’s one:

alias big5="du -h | sort -h | tail -5"

Copyright © 2023 IDG Communications, Inc.