Common Unix Commands

Files/Directories
cp source destination Copies the source file to destination.
ls Listsd the content of the current directory. ls -l – shows also extra info. ls -R – shows content of directory and also subdirectories. ls -m – displays the content in comma sepparated.
mv source destination Moves source to destination (if a directory, else it renames source to destination)
cd directory Changes the current directory. cd ~ – will change the path to the root of the user. cd / – changes the path to the root of the filesystem. cd .. – changes the path to the directory above.
pwd Displays the current directory
mkdir directory Creats a new directory.
rm -R directory Deletes a directory including subdirectories. The user will be promoted for each read-only file.
rm -Rf directory Deletes a directory including subdirectories. The user will NOT be promoted for each read-only file.
rm directory Deletes a file.
chmod entity+mode+permission file/directory Changes the mode of the file in terms od permissions. Entiry= u (for user), g (for group), o (for other), a (for all). Mode = + (for adding permissions) or – (for removing permissions). Permissions= r (for read), w (for write), x (for execute). Example: chmod g+w-r test_file.txt – this adds the write permission to the group but removes the read permission.
chmod user permission_group+group permission_group+all permission_group file/directory Sets the permission for the file, where permission_groups is a number 0-7 which represents a group of permissions – 1-execute, 2-write, 4-read, Example: chmod 765 test_file.txt – this will let the user full permissions to the file but only write+read permission to the group and only read permission for all the rest.
find . –name file name pattern Searchs for file by name, from the current path recursivly.
grep -l text file_pattern Finds all the files which contain the text. Example: grep -l “case” *.java – finds all the java files which contain the word ‘case’.
cmp file file Compares 2 files. Determines whether the files are identical.
diff file/directory file/directory Finds all the diffrences between 2 files or directories.
head file Displays the first 10 lines of a file. head -3 file – displays the first 3 lines of a file.
tail file Displays the last 10 lines of a file. tail -3 file – displays the last 3 lines of a file.
nl file displays the file content with numbered lines.
sort file displays the file content with sorted lines.
strings file Shows only the readable strings of a file.
wc file Displays the number of lines, words and bytes of a file.
file file Gives information about a file.
cat file1 file2 Attaches the 2 files one after another.
more filename Displays a file with paging.
pr -dp Prints a file with paging. pr has a lot of other format abilities.
od binary filename display binary files as eqivalent octal/hex codes
Archives
tar -cvf tar file file1 to be compressed file2 to be compressed file3 t… Compress files into an tar archive.
tar -xpvf tar file Uncompress a tar file.
tar -tf file Lists the contents of a tar archive.
gzip file Compress a single into a gz archive.
gunzip Decompress a single gz archive.
gzcat filename Prints the contents of a zipped file.
System
df Shows disk space information.
quota -v Finding out your available disk space quota
uname -a Gives information about the current system.
du Prints the size on disk of each directory and file.
iostat -xtc Shows the devices(disk/cpu) usage.
top show system stats and top CPU using processes
uptime show one line summary of system status
users
passwd Changes user password.
write user [terminal] Send messges to another user. If no terminal is entered than a list of current used terminals is displayed to choose from.
w Shows who is logged in and what is running.
who Finding out who’s logged on
finger Shows who is logged in. finger name – shows info on user.
whoami who is logged onto this terminal
Network
telnet address Connect to a remote machine.
ssh Connect to another machine.
Processes
ps -a Shows all current processes.
kill process id Kills a process.
kill -9 process id Kills a process by force.
jobs A list of current jobs numbered.
fg job number Brings a job to the forground.
bg job number Brings a job to the background.
kill %job number Kills a job.
command & Runs the command in the background
CTRL-C Stops a process.
CTRL-Z Stops a process temporery.
commands handeling
whereis unix_command Displays the path to a unix command source, binaries and manual page.
command1 | command2 runs command1 and direct it’s output to command2 as input. Example: ls | grep “java”
command1 ; command2 Runs command1 and than runs command2
command1 || command2 Runs command1 and if it fails, runs command2
command1 && command2 Runs command1 and if it succeeds, runs command2
command > file path Writes the output of a file into a file.
man command A command manual.
alias namecommand Creates a new command alisas. Example: alias lj “ls -R | grep java” – this creats a new command ‘lj’ that prints all the java files in the subdirectories.
history list of previously executed commands
command | at -m time. executes command at a time in the future. Example: ( ls > ls.txt ) | at -m 08:02 -> this will create a file containg the contents at exactly 08:02. at -l will print the list of schedualed commands.
Others
script file Starts a script that records all you type into a file. To stop the script press ctrl-d.
date show date and time

Leave a Reply

Your email address will not be published.