Essential Unix Commands for File and Directory Management

Copy a File

  • cp source destination
  • Where source and destination are filenames and may need to refer to full pathnames.
  • Has the effect of copying file source to a new file destination
  • cp source .
  • Has the effect of copying file source into the current directory – the . is shorthand for “here”.

Remove/Delete a File

  • rm file
  • Use this command with caution – files deleted in this way cannot be retrieved.

Rename (Move) a File

  • mv source destination
  • Moves the file source to a new file destination.
  • As well as renaming a file, it can be used to move a file to or from another directory, though full pathnames would be needed for this.

Concatenate the Contents of File(s)

  • cat filename(s) list
  • Concatenates FILE(s), or standard input, to standard output. For example, the content of two files text1.txt and text2.txt is concatenated and displayed on the screen:
  • cat text1.txt text2.txt
  • Offers a quick way to see what is in a file rather than opening it with the text editor
  • more filename
  • Also shows the contents of a file, but pauses after each screen full

Create (Make) a Directory

  • mkdir foldername
  • Creates the directory foldername as a subfolder of the current directory

Create a Directory

  • mkdir path/foldername
  • Creates the directory foldername as a subfolder of the directory described by the path name

Remove/Delete a Directory

  • rmdir foldername
  • The directory must be empty before you can delete it

Change (Call) Directory

  • cd foldername
  • Where foldername may need to be a full pathname
  • cd ~
  • Returns to your home directory from wherever you are
  • cd ..
  • Moves you up one level to the previous directory

Print the Working Directory

  • pwd
  • This command lets you see which directory you are currently in and is useful if you are lost in the directory structure

List the Files and Directories in the Working Directory

  • ls
  • This can also be used to list specific files, e.g. ls ch*
  • ls -l
  • This provides more information about the files/directories.
  • You can also use ls in the form
  • ls, for example

ls /home/cwsubmit/SD1/A*

Lists all of the coursework directories beginning with A.

Repeat a Command

  • Repeats the most recent Unix command
  • !something
  • ! followed by enough letters to uniquely identify a recent command causes that command to be executed again, e.g.

!cd

Would cause the most recent cd command to be executed again.


  • Calling the history commands can be obtained by pressing the arrow buttons.


Get Help Text Related to a Command

  • –help
  • Can also use: man to get detailed information


To Cancel a Command After it Has Been Issued, Press Control-C (Ctrl-C)


Date

  • Displays the time, day of the week, and date

Clear

  • Clears the terminal window

Echo

  • Displays a line of text on the terminal

Grep

  • Extracts lines from a file containing a specified string.

E.g. try typing grep Wed days.txt

Try also: grep nd days.txt

  • You may enclose the search string in single quotes if you wish to include spaces or other special characters.
  • Grep is a very powerful command, especially when used in conjunction with pipes and filters.
  • Type grep -r ‘a’ ~ to find all lines that contain the letter ‘a’ in your home directory (represented by the ~ character) or its subdirectories.

Wildcard Characters

  • Shorthand ways of referring to filenames by using the characters * to replace zero or more characters
  • And ? to replace a single character.
    • E.g – filename ch* would refer to all files which start with ch, such as ch1.txt, ch1.gif, ch2.txt, ch1, ch2, ch3.
    • The filename ch? would only refer to the files ch1, ch2 and ch3.