|
|
PGTS Humble BlogThread: Tips/Tricks For Programming etc |
|
![]() |
Gerry Patterson. The world's most humble blogger |
Edited and endorsed by PGTS, Home of the world's most humble blogger | |
| |
Some Handy Commands |
|
Chronogical Blog Entries: |
|
| |
Date: Mon, 12 Jan 2009 00:21:43 +1100The following is a a list of commands, which I often use when programming. |
(The exception being the last one which is more handy for scripting).
- How to remove a file called "-r"..
If you have GNU rm use:
rm -- "-r"
Otherwise if your rm command doesn't support extended getopt():rm ./-r perl -e 'unlink "-r"'
-
How to sort a directory listing by size.
To sort in increasing size use:
ls -l | sort -nk5
To sort in decreasing size use:
ls -l | sort -rnk5
Note: to use the alias "ll" for "ls -l", put this command in your profile or bashrc:
alias ll='ls -l'
-
How to show a listing of only the directories.
Use this command:
ls -l | grep '^d'
-
How to show a listing of only the files owned by 'foo'.
There are lots of ways to do this, but this is easy to remember:
ll | awk '$3~/foo/'
-
How to delete a file pattern when there is too many files for a shell expansion.
This will delete all files that match 'foo.*' from the current folder and lower levels:
find . -type f -name 'foo.*' -exec rm {} \;
NOTE: if don't want to remove files from the lower levels, use:
find . -maxdepth 1 -type f -name 'foo.*' -exec rm {} \;
-
How to make a tarball with one command if you don't have GNU tar..
For AIX, and HPUX users:
tar cvf - * |gzip -c - > /foo/bar/temp.tar.gz
And to list the contents, use:gzip -dc </foo/bar/temp.tar.gz | tar tvf -
To unpack it, just add the x option:gzip -dc </foo/bar/temp.tar.gz | tar xvf -
-
Using cpio (one of the fastest Unix archive tools)..
Create a cpio archive named /backup/foo.arch.gz containing everything less than 1 day old (without leading '/'), skipping the contents of folders named .foo or filenames ending in '.tmp'
find . -mtime -1 | egrep -v '\/\.foo\/|\.tmp$' | cpio -o | gzip -c - > /backup/foo.arch.gz
To list the contents, use this:gzip -dc /backup/foo.arch.gz | cpio -vt
To unpack it use this:gzip -dc /backup/foo.arch.gz | cpio -ivmd
Warning: If you use "find" with a fully qualified pathname, the leading '/' on the path name will be retained in the archive.
-
How to find out if your (bash or sh) script is being run from a terminal..
If you use a script that is sometimes run by cron and at other times run from the command line, you might want to include code to detect which it is ... Use the tty command to work it out:
tty -s
Examine the return code ($?). If it is 0 then it is a terminal if it is 1 then it is "not a terminal". Your logic can branch from there with the next line of code, or set a variable.