Unix commands Session

~Ranjith

In the layout team, we are learning UNIX command with the support of Sanjeev to improved our skills for automating tasks.

Session on process, kill, disk utility, alias, pushd, popd, tar, and gzip commands:

Process command:

To find process which are all running use “ps” command

ps -ef ; print each (all) process

ps -u <userName>; print process by user

ps -g <groupName>; print process process bu group

groups <username> ; to find group in which user is.

Kill Command:

Used to kill process

kill <signal> <pid> ; send a signal to process <pid>.

kill -l ; signal list;

kill -9 <pid>; -9 is a kill signal

kill -9 -1 ; all process it can.

xkill ; Select the window whose client you wish to kill with button 1….

Disk utility commands:

du -b <path> ; print file space in bytes

du -m <path> ; print file space in megabytes

du -k <path> ; print file space in kilobytes

du -h <path> ; print file space in human readable form.

du -d <depth> -h ; print all files space in given <path> with a given depth.

df -h ; print mounted disk space detail in human readable form

  • To find biggest file:
    • du -b | sort -nr ; sort file based size (bytes), In sort è n = combine digits into number, r = reverse order
  • To find smallest file:
    • du -b | sort -n ; sort file based size (bytes), In sort è n = combine digits into number
Send process into background and bring it foreground:

Press Ctrl+z to suspend process.

Type bg to send process into background

Type “jobs” to get job ID.

Type “fg %<job ID>” to bring process into foreground

Alias: set a alternate name to a command or a group of command within quotes separated “;”

alias ls ls -la –color=auto ; set “ls” as a “ls -la –color=auto”.

alias DoYouWantContinue ‘echo “\nDo you want to continue (Y/N):” ; set input = $< ; if($input != “Y” && $input != “y”) exit’ è set DoYouWantContinue as a set of command separated by “;”.

Unalias <alias Name> ; to remove alias;

/<command> ; to run raw command; even when command name is aliased to something else.

Change directory :

cd <path> ; change directory to given path.

cd – ; last cd path.

pushd <path> ; push directory into stack and cd to it.

pushd +<rotate stack number> ; rotate the stack

popd ; pop the top directory in stack

Create Archieve:

tar -cf <archive.tar> <directory> ; Create archive.tar from <directory>, it is like git version control.

tar -tvf <archive.tar> ; List all files in archive.tar verbosely.

tar -xf <archive.tar> ; Extract all files from archive.tar.

tar -d -f <archieve.tar> <directory>; show the difference between tar and files in directory.

tar -u -f <archieve.tar> <directory>; create a new entry for all files with new time stamp(if file is modified).

tar -r -f <archieve.tar> <directory>; append a new file in tar file.

tar -xvf auto.tar test.tcsh –occurrence=2 ; extract 2nd occurrence of test.tcsh file in auto.tar tree.

Compress:

gzip -c <file> ; compress file

gzip -d <file> ; decompress file

gunzip <file> ; unzip file