Home
All Post
Links
About
Penelitian

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+l to clear the terminal screen and retain already typed text (if any) on command prompt


ls

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
  • -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 in man ls
  • --color=auto list contents with different color for directories, executables, etc

Examples


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 /etc go to ‘etc’ directory under root folder (absolute path specified)
  • cd - switch back to previous working directory
  • cd ~/ or cd ~ or cd go to home directory
    • as specified by HOME environment variable
  • 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_adder create folder project_adder in current directory
  • mkdir project_adder/report create folder report in project_adder directory
  • mkdir -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


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


rm

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


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

  • -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 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_test copy 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


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

  • -f don’t prompt for overwriting and moving write protected files (provided user has appropriate permissions)
  • -i prompt before overwriting

Examples


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

  • -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


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.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 deleted
  • unlink report.log delete link
    • rm report.log can 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 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 version
  • tar cvfz 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.tar
  • tar -xvf backup_mar15.tar extract archived files to current directory
  • tar xvfz 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 output
  • zless 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