Table of Contents
Let's look at commonly used commands to navigate directories, create and modify files and folders. For certain commands, a list of commonly used options are also given
Make it a habit to use man
command to read about a new command - for example man ls
Short descriptions for commands are shown as quoted text (taken from whatis
or help -d
)
print name of current/working directory
- apart from knowing your current working directory, often used to copy the absolute path to be pasted elsewhere, like in a script
- some Terminal emulators display the current directory path as window/tab title by default
$ pwd
/home/learnbyexample
Change the shell working directory
- Like
pwd
, thecd
command is a shell builtin - Let's see an example of changing working directory to some other directory and coming back
- Specifying
/
at end of path argument is optional
$ pwd
/home/learnbyexample
$ # providing an absolute path as argument
$ cd /etc
$ pwd
/etc
$ # to go back to previous working directory
$ # if there's a directory named '-', use './-' to go that directory
$ cd -
/home/learnbyexample
$ pwd
/home/learnbyexample
- Relative paths are well, relative to current working directory
.
refers to current directory..
refers to directory one hierarchy above../..
refers to directory two hierarchies above and so on
$ pwd
/home/learnbyexample
$ # go to directory one hierarchy above
$ cd ..
$ pwd
/home
$ # go to directory 'learnbyexample' present in current directory
$ # './' is optional in this case
$ cd ./learnbyexample
$ pwd
/home/learnbyexample
$ # go to directory two hierarchies above
$ cd ../..
$ pwd
/
cd ~/
orcd ~
orcd
will go to directory specified byHOME
shell variable (which is usually set to user's home directory)
$ pwd
/
$ echo "$HOME"
/home/learnbyexample
$ cd
$ pwd
/home/learnbyexample
Further Reading
- Use
help cd
for documentation - cd Q&A on unix stackexchange
- cd Q&A on stackoverflow
- bash manual: Tilde Expansion
clear the terminal screen
You can also use Ctrl+l
short-cut to clear the terminal screen (in addition, this retains any typed text)
list directory contents
Options
-a
list hidden files also-A
like-a
but excluding.
and..
-1
list in single column (number one, not lowercase of letter L)-l
list contents with extra details about the files (lowercase of letter L, not number one)-h
display file sizes in human readable format-t
sort based on time-r
reverse sorting order-R
recursively display sub-directories-S
sort by file size- directory is treated as file and doesn’t display actual size used by directory, use
du
command if directory size is also needed
- directory is treated as file and doesn’t display actual size used by directory, use
-d
list directory entries instead of contents-q
prints ? instead of non-graphic characters like\n
(Linux file names can use any character other than/
and null character)-F
Append a character to each file name indicating the file type (other than regular files)/
for directories*
for executable files@
for symbolic links|
for FIFOs=
for sockets>
for doors- the indicator details are described in
info ls
, not inman ls
--color=auto
list contents with different color for directories, executables, etc
Examples
ls
list contents of current directory when argument is not givenls /home
list contents of directory home present under the root directory (absolute path specified)ls ../
list contents of directory one hierarchy above (relative path specified)ls -ltr
list files of current directory with details sorted such that latest created/modified file is displayed last- ls Q&A on unix stackexchange
- ls Q&A on stackoverflow
- avoid parsing output of ls
- why not parse ls?
make directories
Examples
mkdir project_adder
create folder project_adder in current directorymkdir project_adder/report
create folder report in project_adder directorymkdir -p project_adder/report
create both project_adder and report directories in one shot- if project_adder already exists, it won't be affected
mkdir /home/guest1
add a home directory for user guest1- mkdir Q&A on unix stackexchange
- mkdir Q&A on stackoverflow
change file timestamps
When a filename is passed as argument to touch
command that doesn't exist, it creates an empty file
More info on this command is covered in a later chapter
touch error.log
creates an empty file error.log in current directory if it doesn't exist- touch Q&A on unix stackexchange
remove files and directories
Options
-r
remove recursively, used for removing directories-f
force remove without prompt for non-existing files and write protected files (provided user has appropriate permissions)-i
prompt before every removal-d
remove empty directories
Examples
rm project_adder/power.log
remove file power.log from project_adder directoryrm -r project_adder
remove folder project_adder from current directory even if non-emptyrm -d project_tmp
remove project_tmp folder provided it is emptyrmdir project_tmp
can also be used
- If available, use
gvfs-trash
command to send items to trash instead of permanent deletion - Files removed using
rm
can still be recovered with time/skill. Useshred
command to overwrite files - rm Q&A on unix stackexchange
- rm Q&A on stackoverflow
copy files and directories
The destination path is always specified as the last argument. More than one source file/folder can be specified if destination is a directory
Options
-r
copy recursively, used for copying directories-i
prompt before overwriting-u
copy files only if newer than existing file in destination location or if file doesn't exist in destination
Examples
cp /home/raja/Raja_resume.doc Ravi_resume.doc
create a copy of file Raja_resume.doc as Ravi_resume.doc in your current directorycp /home/raja/Raja_resume.doc .
create a copy of file Raja_resume.doc in your current directory - name not changed in this case.
represents current directory and..
represents one hierarchy above
cp -r /home/guest1/proj_matlab ~/proj_matlab_bug_test
copy proj_matlab to your home directory as proj_matlab_bug_testcp report/output.log report/timing.log .
copy files output.log and timing.log to current directory- cp Q&A on unix stackexchange
- cp Q&A on stackoverflow
Also check out
rsync
a fast, versatile, remote (and local) file-copying tool- rsync examples
- rsync Q&A on unix stackexchange
- rsync Q&A on stackoverflow
move (rename) files
The destination path is always specified as the last argument. More than one source file/folder can be specified if destination is a directory
Options
-f
don't prompt for overwriting and moving write protected files (provided user has appropriate permissions)-i
prompt before overwriting
Examples
mv project_adder project_lowpower_adder
rename file or foldermv power.log timing.log area.log project_multiplier/result/
move the specified files to result directory- mv Q&A on unix stackexchange
- mv Q&A on stackoverflow
renames multiple files
Note: The perl
based rename
is presented here and different from util-linux-ng version. Check man rename
for details
Options
-f
overwrite existing files-n
dry run without actually renaming files
Examples
rename 's/\.JPG$/.jpg/' *JPG
change the file extension from '.JPG' to '.jpg'rename 's/ /_/g' *
replace all 'space' characters in filenames with '_'- rename Q&A on unix stackexchange
make links between files
Create hard or soft link of file or folder. Soft link is similar to short-cuts created in Windows. Hard link is like same file with different name, same timestamp and permissions of original file. Hard links can be moved to another directory after creation, will still have content even when original file is deleted. On the other hand, soft links have their own timestamps and permissions, it cannot be moved to another folder unless the link creation was done using full path and of course becomes a dead link when original file is deleted. More differences here
Examples
ln -s results/report.log .
create a symbolic link of report.log from results folder to current directoryln results/report.log report.log
create a hard link of report.log from results folder to current directory, will not lose content even if results/report.log file is deletedunlink report.log
delete linkrm report.log
can also be used
- ln Q&A on unix stackexchange
- ln Q&A on stackoverflow
tar
is archiving utility. The archived file is same size as combined sizes of archived files
Usually so often combined with compression utility like gzip
that there is a way to do it just using the tar
command.
Examples
Archive and Compression
tar -cvf backup_mar15.tar project results
create backup_mar15.tar of files/folders project and results-v
option stands for verbose, i.e displays all the files and directories being archived
gzip backup_mar15.tar
overwrites backup_mar15.tar with backup_mar15.tar.gz, a compressed versiontar -cvzf backup_mar15.tar.gz project results
create backup_mar15.tar and overwrite with backup_mar15.tar.gz
Extract archive and Decompression
gunzip backup_mar15.tar.gz
decompress and overwrite as backup_mar15.tartar -xvf backup_mar15.tar
extract archived files to current directorytar -xzvf backup_mar15.tar.gz
decompress and extract archived files to current directory
z commands
zcat story.txt.gz
display file contents of compressed file on standard outputzless story.txt.gz
display file contents of compressed file one screenful at a time- There are other commands as well like
zgrep
,zdiff
,zcmp
etc to work on compressed files
Further Reading