Command Line Working with Files and Directories [Vol-1]
Files and Directories
Let’s look at commonly used commands to move around 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 shown for a command are taken from whatis or help -d
pwd
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
clear
clear the terminal screen
- Use Ctrl+lto clear the terminal screen and retain already typed text (if any) on command prompt
ls
list directory contents
Options
- -alist hidden files also
- -Alike- -abut excluding- .and- ..
- -1list in single column (number one, not lowercase of letter L)
- -llist contents with extra details about the files (lowercase of letter L, not number one)
- -hdisplay file sizes in human readable format
- -tsort based on time
- -rreverse sorting order
- -Rrecursively display sub-directories
- -Ssort by file size- directory is treated as file and doesn’t display actual size used by directory, use ducommand if directory size is also needed
 
- directory is treated as file and doesn’t display actual size used by directory, use 
- -dlist directory entries instead of contents
- -qprints ? instead of non-graphic characters like- \n(Linux file names can use any character other than- /and null character)
- -FAppend 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=autolist contents with different color for directories, executables, etc
Examples
- lslist contents of current directory when argument is not given
- ls /homelist contents of directory home present under the root directory (absolute path specified)
- ls ../list contents of directory one hierarchy above (relative path specified)
- ls -ltrlist 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?
cd
Change the shell working directory
$ whatis cd
cd: nothing appropriate.
$ type cd
cd is a shell builtin
$ help -d cd
cd - Change the shell working directory.
Examples
- cd /etcgo to ‘etc’ directory under root folder (absolute path specified)
- cd -switch back to previous working directory
- cd ~/or- cd ~or- cdgo to home directory- as specified by HOMEenvironment variable
 
- as specified by 
- cd ..go one hierarchy back (relative path specified)
- cd ../..two hierarchy back (relative path specified)
- cd Q&A on unix stackexchange
- cd Q&A on stackoverflow
mkdir
make directories
Examples
- mkdir project_addercreate folder project_adder in current directory
- mkdir project_adder/reportcreate folder report in project_adder directory
- mkdir -p project_adder/reportcreate both project_adder and report directories in one shot- if project_adder already exists, it won’t be affected
 
- mkdir /home/guest1add a home directory for user guest1
- mkdir Q&A on unix stackexchange
- mkdir Q&A on stackoverflow
touch
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.logcreates an empty file error.log in current directory if it doesn’t exist
- touch Q&A on unix stackexchange
rm
remove files and directories
Options
- -rremove recursively, used for removing directories
- -fforce remove without prompt for non-existing files and write protected files (provided user has appropriate permissions)
- -iprompt before every removal
- -dremove empty directories
Examples
- rm project_adder/power.logremove file power.log from project_adder directory
- rm -r project_adderremove folder project_adder from current directory even if non-empty
- rm -d project_tmpremove project_tmp folder provided it is empty- rmdir project_tmpcan also be used
 
- If available, use gvfs-trashcommand to send items to trash instead of permanent deletion
- Files removed using rmcan still be recovered with time/skill. Useshredcommand to overwrite files
- rm Q&A on unix stackexchange
- rm Q&A on stackoverflow
cp
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
- -rcopy recursively, used for copying directories
- -iprompt before overwriting
- -ucopy 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.doccreate a copy of file Raja_resume.doc as Ravi_resume.doc in your current directory
- cp /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_testcopy proj_matlab to your home directory as proj_matlab_bug_test
- cp 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
- rsynca fast, versatile, remote (and local) file-copying tool
- rsync examples
- rsync Q&A on unix stackexchange
- rsync Q&A on stackoverflow
mv
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
- -fdon’t prompt for overwriting and moving write protected files (provided user has appropriate permissions)
- -iprompt before overwriting
Examples
- mv project_adder project_lowpower_adderrename file or folder
- mv 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
rename
renames multiple files
Note: The perl based rename is presented here and different from util-linux-ng version. Check man rename for details
Options
- -foverwrite existing files
- -ndry run without actually renaming files
Examples
- rename 's/\.JPG$/.jpg/' *JPGchange the file extension from ‘.JPG’ to ‘.jpg’
- rename 's/ /_/g' *replace all ‘space’ characters in filenames with ‘_’
- rename Q&A on unix stackexchange
ln
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 directory
- ln results/report.log report.logcreate a hard link of report.log from results folder to current directory, will not lose content even if results/report.log file is deleted
- unlink report.logdelete link- rm report.logcan also be used
 
- ln Q&A on unix stackexchange
- ln Q&A on stackoverflow
tar and gzip
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 resultscreate backup_mar15.tar of files/folders project and results- -voption stands for verbose, i.e displays all the files and directories being archived
 
- gzip backup_mar15.taroverwrites backup_mar15.tar with backup_mar15.tar.gz, a compressed version
- tar cvfz backup_mar15.tar.gz project resultscreate backup_mar15.tar and overwrite with backup_mar15.tar.gz
Extract archive and Decompression
- gunzip backup_mar15.tar.gzdecompress and overwrite as backup_mar15.tar
- tar -xvf backup_mar15.tarextract archived files to current directory
- tar xvfz backup_mar15.tar.gzdecompress and extract archived files to current directory
z commands
- zcat story.txt.gzdisplay file contents of compressed file on standard output
- zless story.txt.gzdisplay file contents of compressed file one screenful at a time
- There are other commands as well like zgrep,zdiff,zcmpetc to work on compressed files
Further Reading
