Skip to content

Latest commit

 

History

History
335 lines (235 loc) · 12.7 KB

Files_and_Directories.md

File metadata and controls

335 lines (235 loc) · 12.7 KB

Files and Directories

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)


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
  • some Terminal emulators display the current directory path as window/tab title by default
$ pwd
/home/learnbyexample

cd

Change the shell working directory

  • Like pwd, the cd 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 ~/ or cd ~ or cd will go to directory specified by HOME shell variable (which is usually set to user's home directory)
$ pwd
/
$ echo "$HOME"
/home/learnbyexample

$ cd
$ pwd
/home/learnbyexample

Further Reading


clear

clear the terminal screen

You can also use Ctrl+l short-cut to clear the terminal screen (in addition, this retains any typed text)


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


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 -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.tar
  • tar -xvf backup_mar15.tar extract archived files to current directory
  • tar -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 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