Linux Command Line : One Liners
Posted by raxsoAug 2
Suppose you want to know how many files are in the current directory.
ls | wc -l
Suppose you want to know about the five processes that are consuming the most CPU time on your system:
ps -eo user,pcpu,pid,cmd | sort -r -k2 | head -6
The ps command’s o lets you specify the columns that you want to be shown. sort -r does a reverse order sort with the second column (pcpu) as reference (k2). head gets only the first six lines from the ordered list, which includes the header line. You can place pcpu as the first column and then omit the k2 option because sort by default takes the first column to do the sort.
A common situation for Linux administrators on servers with several users is to get quick ordered user lists. One simple way to get that is with the command:
cat /etc/passwd | sort
If you just need the username, the above command returns too much information.
cat /etc/passwd | sort | cut -d":" -f1
The sorted list is passed to cut, where the d option indicates the field’s delimiter character. cut breaks into pieces each line, and the first field f1 is the one that you need to display. That’s better; it shows only usernames now. But you may not want to see all the system usernames, like apache, bin, and lp. If you just want human users, try this:
cat /etc/passwd | sort | gawk '$3 >= 500 {print $1 }' FS=":"
gawk evaluates each line from the output piped to it. If the third field — the UID — is equal or greater than 500 (most modern distros start numbering normal users from this number) then the action is done. The action, indicated between braces, is to print the first field, which is the username. The separator for field in the gawk command is a colon, as specified by the FS option.
Now suppose you have a directory with lots of files with different extensions, and you want to back up only the .php files, calling them filename.bkp. The next one-liner should do the job:
for f in *.php; do cp $f $f.bkp; done
This command loops through all the files in the current directory looking for those with .php extensions. Each file’s name is held in the $f variable. A simple copy command then does the backup. Notice that in this example we used a semicolon to execute the commands one after another, rather than piping output between them.
What about bulk copy? Consider this:
tar cf - . | (cd /usr/backups/; tar xfp -)
It creates a tar package recursevely on the current directory, then pipes this package to the next command. The parenthesis creates a temporary subshell, changes to a different directory, then extracts the content of the package, which is the whole original directory. The p option on the last tar command preserves file properties like time and permissions. After completion, the shell context will be at the original directory.
A variant on the previous one-liner lets you do the same kind of backup on a remote server:
tar cf - . | ssh smith@remote.server tar xfp - -C /usr/backup/smith
Here, the command establishes an SSH remote session and untars the package with the C option, which changes the directory, in this case to /usr/backup/smith, where the extraction will be made.
Like this blog? Why not buy me a cup of coffee?




![[hackers black book]](http://raxso.net/images/hbb-ani-misuse.gif)





Good to see someone posting command line stuff. Seems like no one uses the command line anymore! It’s a shame because it’s such an elegant way to get things done. Windows has made us slow and stupid! I even have it on two of my computers now! I’m getting soft, but it works for web browsing I guess. However if I want to get some serious work done, I still use UNIX or Linux.
[Reply]