Blog Post

Unix Basic Commands


In similar context to my previous blog I'm providing a list of UNIX basic commands which you may find handy if your new to the UNIX world.

UNIX Help Command

  • man <command>
  • man man –gives details of the man command

UNIX File Operations Commands

Piping operators for I/O redirection

  • >file -Make file the standard output
  • <file -Make file the standard input
  • >>file -Make file standard output & append its contents
  • <<word -Take shell input upto the first line containing the word or EOF
  • Cmd1 | cmd2 –Take the output of cmd1 & supply it as input for cmd2.

Viewing a file 

  • Find /dir1 /dir2 –name A* -print – Finds all the files beginning with A in the directories dir1 & dir2 & prints them
  • Cat <file>
  • Touch <filename> -Create an empty file
  • Cat file1 file2> file3 - File 3 if existing will get overwritten with contents of file 1 & then file 2.File 3 if non-existent will be created with the above contents.
  • >>file3- Contents of file3 will be appended instead of overwritten.

Copying a file

  • Cp source_file1 destination_file2 - Copies the source file1 to destination file2
  • Cp file1 file2 file 3 /etc/bin/dump - Copies the list of files (1,2,3) in current directory to the folder /etc/bin/dump
  • Cp /etc/tmp/file1 /etc/dump/file1

Removing a file (In windows context-deleting a file)

  • Rm file1 
  • Rm –I file1 - You are asked confirmation to delete the file.
  • Rm –r dir1 - Recursive directory content deletion

Moving a file (In windows context-renaming a file)

  • Mv file_old file_new – Renames file_old with the new name file_new
  • Mv dir_oldname dir_newname –Renames directory in similar fashion

Listing files

  • Ls – Lists files in the alphabetical order
  • Ls –l - Long listing of files
  • Ls –A –Show hidden files as well

Permissions Weight

Permissions  Weight
Read (r)           4
Write (w)        2
Execute(x)       1

Assigning permissions to a file

  • Chmod 741 myfile – Grants owner (4+2+1) all permissions on my file. Grants group of the owner (4) read permission on the file.Grants rest of the users (1) execute permission on the file 
  • Chmod go+rw u=r file1- Add the read & write priveledge to owner & group users. Grants only read permission to the rest of the users to file1 
  • syntax(chmod [o,g,u] [+/-/=] [permissions] file

-Read permission required for a directory to view the files it contains.
-Execute permission required for a target directory to change the current directory to the target directory.

Commands for directories

  • Mkdir dir_name – Creates the directory in the current working directory
  • Pwd - Returns the current working directory
  • Rmdir dir_name- Removes the directory provided its empty

UNIX File Manipulation Commands

Metacharacters used for manipulation operations

  •  ?- symbol signifies a single character
  • *- Inpresence or absence of any number of character which exists at the place of *.
  • [abcd]*- matches records starting with the range (in this case a,b,c,d)
  • ![abcd]*-matches records not starting with the above range.

Commands for viewing contents of the files

  • Wc -lwc file1 -Counts the number of lines, words & characters in file1
  • nl file1 -Add line numbers to the file1
  • Cat file1 -Concatenates & displays files 
  • Head -15 file1 -Displays the first 15 lines of file1
  • Tail -15 file1 -Displays the last 15 lines of file1
  • Uniq file1 -Reports repeated lines of file1

Viewing modified contents

  • Sort file1 file2 file3 -Sorts contents in all the 3 files alphabetically and displays output in terminal.
  • Sort –m –oresults file1 file2 -Merges output for already sorted files (file1,file2) and sends output to file results.
  • Sort –r +1 -3 file3 - Reverse sort starting at the 2nd field (+1), ending at 3rd field(-3) for File3.
  • Cut –f 2,3 –d “:” file1 -Cut picks the 2nd & 3rd field for the file1 delimited by the : (colon) symbol & displays output at the terminal.
  • GREP (globally search an expression & print it)
  • Grep ‘search this pattern’ –i-n file1 file2. Searches for the pattern in quotes for file1 & file2.   -n will include the entire line to be displayed. –i makes it case sensitive.
  • Who |tee logfile| sort - Tee is used in place where the output is to be redirected to a file as well as supplied as an input to the next command. Here tee command will send the output of ‘who’ command to logfile as well as supply the same output to the ‘sort’command & display the sorted results in terminal.

UNIX Terminal Users commands

  • Logname –Prints the logname of the user
  • Uname –Prints the name of the unix system
  • Uname –m - Prints the machine model ( eg: i686)
  • Uname-r -Prints the kernel version
  • Hostname -Returns hostname of the machine
  • Hostname <machinename> -Returns the ip of the machine.
  • /sbin /inconfig – as good as ipconfig for windows
  • Rsh <host> <command> -Execute command on host.
  • Ssh <host> -Start terminal session on host
  • telnet <host> -start terminal session on host
  • Sync -Forces the buffer cache & the page cache to write all the dirty pages to the disk.
  • Shutdown/shu <mode> <delay>- Does a graceful shutdown of the system, modes -r for reboot,-h for halting, delay accepts number in seconds to wait till shutdown.Else mention ‘now’ to send a warning message to all users about shutdown.
  • Dmesg -View boot messages
  • Cat /etc/passwd -Lists information for all the users on the system
  • Printenv /enc- prints all the environment variables

UNIX process related commands

  • Ps -a -Lists all the processes; from all the users (-a option) that are running at the current instance. 
  • Ps –f –t tty3 – Shows a full listing of all the processes that are running from terminal (tty3).
  • Ps –u user1 –Shows all the processes that are running under the user (user1).
  • Daemon processes are constantly running in the background.
  • Sort file1 > file2& - this command will be forced to run in the background due to the ‘&’ symbol at the end & will return the PID number when you hit enter.

· Note: use the PID to track the completion of the process. The output should always be redirected to a file.

  • Nohub sort file1> file2 –the command is executed in the background & guaranteed execution completion even if we logout.NOHUB stands for no hang ups
  • At <option> time – Runs the job specified by <option> at the time. Once you hit enter after the ‘at’ command you may enter the job definition. Once you hit Ctrl+d after giving the job details you will receive the job id number. Options –l is used for listing of the jobs & -r for removing of the jobs
  • At now+1 day <cmdfile - now the ‘at’ command will execute the commands in the cmdfile after a day.
  • /users/lib/cron/at.allow file contains the list of users who are permitted to execute ‘at’command. Similarly at.deny file contains list of users who are not permitted to execute the ‘at’ command
  • Batch <option> time –Runs the command when the load average drops below 8,optionally after the set Time.
  • Crontab cmdfile –executes the job script in cmdfile at the schedule mentioned in the cmdfile. Options –l is used for listing of the jobs & -r for removing of the jobs

Time format : Minute |Hour| DayofMonth|Month of the year|Day of the Week|Command
Eg : cat >cmdfile

30 9 1 * * echo “work hard”
Ctrl +d
Crontab will echo the message at 9.30AM (30 9 1 * *) on first of every month (30 9 1 * *)
‘*’ denotes all possible values. Sunday is represented as ‘0’ in the ‘Day of week’

  • /users/spool/cron/crontabs stores the lists of crons scheduled 
  • /users/lib/cron/cron.allow file contains the list of users who are permitted to execute ‘crontab’command. Similarly cron.deny file contains list of users who are not permitted to execute the ‘crontab’ command

UNIX important directories

  • /etc/shadow –Contains the encrypted passwords for the user. Only viewable by the root user.
  • /var/long - Location for most of the system log files
  • /etc/inittab – Location of system configuration files
  • /etc/brc -Executed at startup by init
  • /etc/hosts –List of hosts that the system is aware about.
  • /usr/lib/cron/log –Log of cron processing
  • /usr/spool/cron/crontabs –Event list for cron
  • /etc/mnttab -List of mounted file systems
  • /etc/rc -Startup shell script executed by init
  • /etc/profile –Custom shell script executed by init
  • /etc/sysctl.conf –Compiled in kernel parameters

UNIX file system related commands

  • mount <option> <dev> <mount_point> -Mounts a file system on device <dev> at <mount_point> with the <options> specified.
  • unmount <dev>/<mount_point> -Unmounts the file system or device.

UNIX system monitoring commands

  • lsmod -Show currently loaded modules
  • insmod -Load a module
  • rmmod –Unload a module
  • ipcs <option> - Option –m shows the current usage of shared memory, -s usage of semaphores
  • du -Reports directory-wise disk usage
  • df -Reports directory-wise disk free space
  • dfspace -Reports free disk-space in terms of MB.
  • sar <option> <interval> <count> - Reports the system activity, option –q shows the CPU 
  • queue, -u shows the CPU utilization, -d shows the device activity.
  • vmstats <interval> <count> -Shows the virtual memory stats
  • iostats <option><interval><count> -shows the i/o statistics, option –d for device activity. -c        
  • for CPU activity, -x for extended disk activity.


You rated this post out of 5. Change rating




You rated this post out of 5. Change rating