Basic Shell Commands
1) Listing Directory Contents
ls Display all files and folders in the current directory
ls -a Displays all files including hidden
ls -l Display all files along with size and time stamp
ls -ltr Display in long listing format with timestamp in reverse order 2
2) Moving around the file system
pwd Displays the "present working directory"
cd Changes current directory to "HOME" directory
cd desktop Looks for a directory name desktop in the current working
directory and changes to that 3
cd /home/cloudera Changes current directory to /home/cloudera
3) Create a directory
mkdir dezyre Creates a directory dezyre in the current directory
4) Create a copy of file in same directory
cp file1 file2 Creates a copy of file1 in the same directory with name file2 4
5) Create a copy of file in different directory
cp file1 backup/file2 Creates a copy of file in directory /backup with
name file2
6) Rename a file
mv file1 newfile Renames file1 to newfile 5
7) Move file from one directory to another
mv file1 backup/file2 Moves the file1 from current directory into
subdirectory backup with name file2 - file1 will no longer exist in the current
directory
8) Delete a file
rm file1 Deletes file1 from current directory 6
9) Delete an empty directory
rmdir backup Deletes directory backup only if its empty
10) Delete a directory with files
rm -r dezyre Deletes dezyre and all its files and subfolders 7
11) Viewing files
cat file1 Will display contents of file1 on screen
more file1 Used to display the contents of a large file (ENTER = one line
down; SPACE = page down; q=Quit)
head file or head -2 file Will display the first few lines of a file or if -2 is
given - will display the top 2 lines
tail file or tail -2 file Will display the last few lines of a file or if -2 is
mentioned - will display the last 2 lines 8
12) Editing files
vi file1 Will open vi editor to editor to edit file (:q = Quit; :wq = Write and
Quit;
For vi commands - you can refer to links below
http://www.cs.rit.edu/~cslab/vi.html
http://www.cs.colostate.edu/helpdocs/vi.html
http://www.tutorialspoint.com/unix/unix-vi-editor.htm 9
13) Size of files
du -h dezyre/
du -h dezyre/*
10
14) Change group of file/directory
Create a group dezyre_group and a user dezyre_user
sudo addgroup dezyre_group
sudo adduser --ingroup dezyre_group dezyre_user
11
Group for sales.txt is cloudera
Change Group for sales.txt to dezyre_group
sudo chgrp dezyre_group sales.txt
12
15) Change file/directory permissions
chmod 777 sales.txt 13
16) Change owner
The owner and group of file employee.txt is cloudera.
sudo chown dezyre_user:dezyre_group employee.txt