Some Very Cool Unix Commands that might be real helpful for an interview or for anything while working with Unix or OS X.
cd (change directory) |
cd myfolder |
Changes the current working directory to “myfolder” |
cd .. |
Go up one level to the current working directory. |
cd ../.. |
Go up two levels to the current working directory. |
cd / |
Changes the current working directory to the root directory. |
cd ~ |
Changes the current working directory to your home directory. |
mkdir (Make directory) |
mkdir freshershome |
Create a new folder called “freshershome” in the current directory. |
mkdir /freshershome |
Create a new folder called “freshershome” in the root directory. |
mkdir ~/freshershome |
Create a new folder called “freshershome” in your home directory. |
ls (list) |
ls |
list the file names in the current working directory. |
ls -l |
list the file names with “long” description/information (size, privilages, etc) |
ls -a |
list “all” file names in the current working directory including the hidden files. |
ls -l *.jpg |
list the file names ending in “.jpg” and display it in “long” description format(-l) |
cp (copy) |
cp letter.txt newletter.txt |
Copy the file called “letter.txt” and name it “newletter.txt” in the current directory |
cp my.cnf /etc/my.cnf |
Copy the file “my.cnf” and put it inside the root -> etc folder. |
cp tax05.db ~/Taxes |
Copy the file name “tax05.db” and put it inside my home directory -> Taxes folder |
cp “.jpg ~/freshershome |
Copy all the files with “.jpg” extention and put them inside my home directory -> freshershome folder |
cp -R ~/Docs /backups/’Docs Backup’ |
Copy the entire “Docs” directory from my home page and put it inside the root -> backups and call it “Docs backup” (use quotes if you use folder names with space. example: ‘Docs Backup’ (-R stands for “Recursive”) |
sudo cp -Rp /Users /UsersBackup |
Copy the entire “Users” folder including subfolders and files, preserve owner, group, permissions, and timestamps and save the new folder in the root -> UsersBackup location. “-Rp” stands for Recursive Preserve:
Recursive: Copy include subfolders and files.
Preserve: Preserve owner, group, permissions, and timestamps information.
(use “sudo” to get root access temporarily.) |
mv (Move or Rename) |
mv /letter.txt ~/letter.txt |
Move the file “letter.txt” from the root directory to the home directory. |
mv badletter.txt niceletter.txt |
Rename the file “badletter.txt” to “niceletter.txt” in the current directory. |
mv Pictures freshershome |
Rename the folder “Pictures” to “freshershome” in the current directory. |
mv *.jpg ~/freshershome |
Move all the files with “.jpg” extention and put them inside my home directory -> freshershome folder |
rm (Remove) |
rm letter.txt |
Delete the file “letter.txt” from the current directory. |
rm ~/Badfreshershome/*.jpg |
Delete all the files with the ‘.jpg’ extention inside your home directory -> “Badfreshershome” folder. |
rm -R Temp |
Delete the “Temp” directory and all of its contents in the current directory (-R stands for “Recursive”) |
rm -fr Temp |
Delete the “Temp” directory and all of its contents including write-protected files without prompting in the current directory (-f stands for “force” -r stands for “recursive”) |
find (files and folders) |
find ~ -name myletter.doc -print |
Search for the file names “myletter.doc” inside my home directory and print the result to the screen |
sudo find / -name mysql -print |
Search for the file and folder names “mysql*” starting from the root directory and everywhere within it and print the result to the screen. (use “sudo” to get root access temporarily.) |
find . -name myletter.doc -print |
Search for the file names “myletter.doc” inside the current directory and print the result to the screen |
find . -name ‘myletter*’ -print |
Search for the file names starting “myletter” inside the current directory and print the result to the screen |
locate (similar to find) |
locate ~ -name myletter.doc |
Search for the file names “myletter.doc” inside my home directory and print the result to the screen |
pwd (print working directory) |
pwd |
Displays the pathname of the current working directory. |
who (who logged in) |
who |
Displays who is logged into the system. |
who am i |
Displays my user name. |
who -uH |
Displays who is logged into the system including heading “H” and idle time information. |
su (set user) – type exit to switch back to your own identity |
su |
Temporarily become the root user. (this will give you root access privilages and the most control over the OS) – it will prompt you for the administrator password. |
su username |
Temporarily become another user called “username” (replace “username” with the user that you wish to use as your new identity – this will give you access privilages for the “username”) – it will prompt you for the that user’s password. |
sudo (set user and do . . . . . . ) – similar to su except ‘su’ will give you prompt but ‘sudo’ you can start typing commands right after the ‘sudo’ command. |
sudo find / -name mysql -print |
Temporarily changes your identity to the root user so you can search for all the files including the once that require root access privilage. It prompts you for administrator/root password |
sudo Bobuser rm /Users/Bobuser/freshershome/myphoto.jpg |
Temporarily changes your identity to the “Bobuser” identity so you can delete a photo named “myphoto.jpg” from the home directory -> freshershome folder belonging to Bobuser – It prompts you for “Bobuser”‘s password. |
ps (running processes) |
ps -ax |
List all running processes |
ps -aux |
List detailed information on all running processes. |
top (CPU-intensive processes currently running) – press the “q” key to quit the “top” utility |
top |
List all running processes sorted by process id – descending and updating every second – don’t forget to press the “q” key to quit, otherwise it will run continuously. |
top -us10 |
List all running processes sorted by CPU usage – descending and updating every 10 seconds – don’t forget to press the “q” key to quit, otherwise it will run continuously. |
kill |
kill 160 |
Tell the process ID #160 to terminate. |
kill -9 160 |
Terminate the process ID # 160 at once without any hesitation. |