Working with Files and Directories on the Unix Command Line
Getting Started
It doesn't take too many commands to be able to accomplish most of what you will need to work with files and directories on the command line. Each of the commands below that use either a directory name or file name can be used with specifying a relative or absolute path.
Navigating the File System
You need to know both what directory you are in and how you change to a different directory.
The Working Directory
To print the directory you are in, use the pwd (Print Working Directory) command.
pwd
The directory you are in is referred to as the "working directory."
Changing Directory
To change to another directory, use the cd (Change Directory) command followed by the directory you want to change to. You can use relative paths or absolute paths. If you have a directory called "Desktop" inside your working directory, you can change to it with the following:
cd Desktop
Or you can use an absolute path.
cd /home/myuser/Desktop
To go back a directory, you can use the following:
cd ..
You can also use cd just by itself. This will take you to your home directory.
cd
Working With Directories
You first need to know how to work with directories before you can learn about files.
Viewing Contents
To view all of the files and directories in a directory, use the ls (List) command.
ls
This will list all files and directories in the working directory.
To list all the files in a long, detailed format, add the -l argument to the command. This will show detailed information about each file and directory.
ls -l
Creating Directories
To create a directory, use the mkdir (Make Directory) command followed by the name of the directory you want to create.
mkdir docs
This creates the docs directory inside the Desktop directory.
You can use the -p parameter to make the parent directories if they do not exist.
mkdir -p path/to/my/files
This will create each directory in the path if they do not exist.
Deleting Directories
To delete a directory and all of the contents in it, use the rm (Remove) command with a -r argument, followed by the directory name.
rm -r docs
This will delete all contents in the docs directory. There isn't a way to undo what you have deleted, so use caution when using this.
You can also use the rmdir (Remove Directory) command followed by the directory name to delete directories. This will only work with deleting empty directories.
rmdir docs
If you try to delete a directory that isn't empty with this command, you will get an error.
Moving Directories
To move a directory, you use the mv (Move) command followed by the directory you are moving and the directory to move that directory to.
mv docs myBusinessDocs
This moves the docs directory into the myBusinessDocs directory.
Renaming Directories
To rename a directory, you use the same mv command as above.
mv docs businessDocs
This will rename the docs directory to businessDocs.
Copying Directories
To copy a directory and all of its contents, you use the cp (Copy) command using the -r argument, followed by the directory to copy and the directory to copy it to.
cp -r docs businessDocs
This copies the docs directory and all of its contents into the businessDocs directory.
Working With Files
Working with files is similar to working with directories. Some of the commands are shared between both files and directories.
Viewing Contents
To view the contents of a file, use the cat (Concatenate) command followed by the name of the file.
cat todo.txt
This will print the contents of the todo.txt file in the console.
Creating Files
To create a file, you use the touch command followed by the name of the file you want to create.
touch todo.txt
This will create an empty file called todo.txt in the working directory.
Deleting Files
To delete a file, you use the rm (Remove) command followed by the file name to delete. This is the same command to delete directories.
rm todo.txt
This will delete the todo.txt file in the working directory.
Moving Files
To move a file, you use the mv (Move) command followed by the file you are moving and the directory to move that file to.
mv todo.txt myBusinessDocs
This moves the todo.txt file into the myBusinessDocs directory.
To move multiple files, you can specify a space-separated list of files.
mv todo1.txt todo2.txt todo3.txt businessDocs
This moves todo1.txt, todo2.txt, and todo3.txt files into the businessDocs directory.
Renaming Files
To rename a file, you use the same mv command as above.
mv todo.txt completed.txt
This will rename the todo.txt file to completed.txt.
Copying Files
To copy a file, you use the cp (Copy) command followed by the file name and the directory to copy it to.
cp todo.txt businessDocs
This copies the todo.txt file in the working directory to the businessDocs directory.
To copy multiple files, you can specify a space-separated list of files.
cp todo1.txt todo2.txt todo3.txt businessDocs
This copies todo1.txt, todo2.txt, and todo3.txt files to the businessDocs directory.
Conclusion
Working with files and directories is pretty simple on the command line. The commands covered will do most of what you need to be able to do on the command line.