Files and Directories

pwd – Display name of current directory

The command pwd is used to display the full path name of the current directory.


cd – Switch to another directory

To switch to another directory, the command cd is used.

Examples What it does
cd Will place you in your home directory
cd / Will move you to the root directory
cd /etc Will move you to the /etc directory
cd ../ Will move you back one director

top of page


mkdir and rmdir – Create/Delete directories

The command mkdir is used to create a new directory.

The command rmdir or rm -r is used to delete a directory or directories.Be careful in testing the following delete commands. You will probably want to create sample directories first.

Examples What it does
mkdir mydirectory Will create a new directory named ‘mydirectory’
rmdir existingdirectory Will delete the existing directory named existingdirectory’
rm -r existingdirectories Will delete the existing directory named ‘existingdirectories’ and all directories and files below it.

top of page


ls – List the Contents of a directory

The command ls is used to the contents of a directory.

Options What it does
-l long listing
-R list current directory and all other directories within current directory
-a list hidden files
-CF list in column format and append ‘*’ to executable files,
‘@’ to symbolic linked files, ‘/’ to directories
-r list in reverse alphabetically order
-t list more recent accessed files first
filename(s) Values to match
Examples What it does
ls only list file/directory names in current directory
ls -l list all file/directory information in current directory(long version)
ls -R list all files in current directories and below
ls -lt list all files, sorted by most recent accessed first
ls -lt /etc/rc* list files in the ‘/etc/ directory, only starting with ‘rc’ and
sort results by most recent

top of page


Wildcards

Wildcard characters are used to help find file or directory names

Options What it does
* asterisk symbol is used to represent any character(s)
? question mark is used to represent any single character
[from-to ] Values entered within square brackets represent a range
(from-to) for a single character
[!from-to ] Values entered within square brackets represent a range (from-to) to exclude for a single character
Examples What it does
a* all files starting with the letter ‘a’
*z all files where the last character is a ‘z’
a*m all files that start with the letter ‘a’ and end with ‘m’
th?? all files that start with ‘th’ and are only four characters long
[a-c]* all files that start with ‘a, b or c’
x[A-C]* all files that start with the letter ‘x’ and the second character contains ‘A, B or C’
[!M-O]* all files except those that start with ‘M, N or O’

top of page


cp – Copy files

To copy a file, the command cp is used
Example: cp oldfile myfile – Will copy the existing file ‘oldfile’ to a new file ‘myfile’
top of page


mv – Rename files

The command mv is used to rename a file
Example: mv myfile yourfile – Will rename the file ‘myfile’ to ‘yourfile’
top of page


rm – Delete files

Examples What it does
rm myfile remove the file ‘myfile’
rm -i abc* prompt to remove each file in current directory starting with  ‘abc’
rm abc* remove all files in current directory starting with ‘abc’ automatically

top of page


wc – Count the number of lines or characters

The command wc is used to count lines, words or characters in a file or piped results from another command.

Options What it does
-c Number of characters
-w Number of words
-l Number of lines
filename file name(s) to use
Examples What it does
wc /etc/sendmail.cf Lists the number of lines, words and characters in the file ‘sendmail.cf’
ls /etc | wc -l Lists the number of files and directories in the directory ‘etc’

top of page


file – Display Type-of-File Description

Files can consist of several types. The command file is used to display a description for the type.
Example: file a* will list all files in the current directory that start with the letter “a” and provide a description for the file type.
top of page


cat – concatenate files

The command cat is a multi-purpose utility and is mostly used with TEXT files.

  • Create a new file and optionally allow the manual entry of contents
    • cat >myfile will create a file named myfile and allow you to enter contents.
    • Press Control-D to exit entry mode.
    • WARNING: If “myfile” already existed, this command would replace the old file with the contents of the new file.
  • Combine text files
    • cat file1 file2 >newfileThis will combine file1 and file2 into newfile.
  • Display the contents of a file
  • cat myfile
  • Delete the contents of a file
  • cat /dev/null >myfile

top of page