Unix commands

man [command] shows man page
apropos [command] shows command description (greps the descrip lines for given string) = man -k [command]
w find users logged in and their activity
cal show simple calendar
cal 2017 get full year calendar for 2017
cal 10 2005 shows the month of Oct, 2005
ls lists dir contents
  -s list total files and no. files in sub dirs
   -sd lists total no of files only
   -s [dir] lists contents of [dir] and the no of files in its sub dirs
  -t list most recently accessed files first
  -R lists files recursively
  -l long list files in a dir
  -m list comma separated
  -x list by rows
   -r list in reverse order
  --color=auto --group-directories-first list dir/files horizontally
  -lh --color=auto --group-directories-first shows dir/file list with permissions, owner, file size, date modified
  -alhrt shows dir/file list sorted recently modified first
  -alh shows dir/file list including hidden
  -d .* --color=auto shows only hidden files
  -lhd .* --color=auto
  -Rh shows files recursively
cp {file} {newfile} copies file with new name
   -u update switch copies only if source file newer than dest file
mv {file} {dir}/ moves file to another dir
mv {file} {newfile} changes the file's name (moves to new file, deletes old)
mkdir -p creates dir with necessary parent dir's
du disk usage of all dir below location
   -s total disk usage only
   -a include size of all files
df disk free space on system
touch {file} creates the file, or modifies date-time to current on file
compress {file} compresses file to .z
uncompress {file.z} uncompresses file.z
rm -i [file] interactive remove
   -r [dir] recursive removal of a dir
   -v [file] verbose -explains what is being done
file [file] [dir] shows what type a file or dir is
head -10 [file] view 1st 10 lines of file
head -5 [file] [file] show for multiple files
tail -10 [file] show last 10 lines of file
head -50 [file] | tail -10 shows the lines 41-50 of file (using a pipe)
cat [file] displays file content (entire)
more [file] displays file content part-by-part
   -cs [file] -c display clears screen each new page and -s suppresses blank lines
more +20 [file] displays from line 20
more +/[text] [file] displays from certain text in file
   {n}Enter scroll n lines down
   d scroll down half a page
   b scroll back half page
   h list commands in more program
< use this to pass input file to the command
> use to send output of command to a file
>> use to append output to a file
wc [file] shows no. of lines, words, characters in a file
wc -l counts only lines...
[command] | wc -l now wc counts the lines output from the command and gives youa number (command e.g. ls, who)
cat -n [file] shows file with line numbers
nl [file] does the same
   -ba [file] numbers blank lines
   -s', ' [file] changes default tab after number to comma and space
echo boowrites "boo" to screen
echo boo >> [file] appends "boo" to a file
echo *list all files in the dir
echo b*list all the file in dir beginning with b
  ? single character wildcard (* can be any length of characters)
ls [a-z]* lists all file/dir beginning with a letter
grep [word] [file] display lines from a file with a certain word or text
grep 'ho*' [file] displays lines containing "ho" in any word
  -i ignores case of search criterea
  -n numbers the lines that are matched
  -c count of matching lines
  -l lists files with matching lines
sed -i 's/cap/hat/g' [file] sed substitutes cap with hat in a file (the -i option replaces inline, in the file, without which the changes are printed to standard output, the screen)...
grep 'cap' [file] | sed -i 's/cap/hat/g' another method (g tells sed to do every ocurrance)
sed -i 's/cap/hat/g;s/cat/rat/g' sed carries out 2 different substitutions, and every instance of each

UNIX in 24 hours

- by Dave Taylor. James C. Armstrong, Jr.
This is a useful book to look at if you are new to Linux, or haven't read up properly about the basic functions of the UNIX OS. But its no longer around for free!
download the pdf here

Some more technical stuff

move files found in a list of file names (files.list)
while read file ; do mv "$file" /dir/dir/moved/ ; done < files.list

replace all text occurring in all files under the current dir
find . -type f -exec sed -i 's/abc/xyz/g' {} +
or using perl:
find . -type f | xargs perl -pi -e 's/abc/xyz/g'

or in files in the current dir only (and of certain extension)

sed -i -- 's/abc/xyz/g' *.py

in a specific file
sed -i 's/old/new/g' filename

There is also a program called rpl..

rpl -iw old new filename

where -i ignores case, -w finds whole words only, -x specifies extensions of files to replace in, -p is interactive mode, -e regular expression escapes, -d preserve modification times, -s simulate, -v verbose.

replace a whole line in a file
sed 'Ns/.*/replacement-line/' filename

N=line no.

change file names
find . -depth -name "* *" -execdir rename 's/oldname/newname/' '{}' /;

Note that the "* *" will find all file names with spaces and then excecute the name change operation on only those files found. To include files without spaces in the name, just use "**".

To narrow down the search and speed up the operation further, especially if the directory tree contains thousands of files, if its a certain string to be changed, such as "ee", then use "*ee*".

Be careful using "y" e.g. 'y/sring/string2/' as it does a translate action, not a direct replacement, it may even replace letters/digits one for one, e.g. 'y/b45/c54/' will change all 'b's for 'c's, all '4's for '5's etc - so you can end up with totally wasted file names if you use it without knowing what its doing.

Home | Content | Site Map | TOP