Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and delete them from the command line is very important and quite powerful. These file manipulation commands allow you to perform the same tasks that a graphical file explorer would perform.
Create an empty text file called myFile
:
touch myFile
Rename myFile
to myFirstFile
:
mv myFile myFirstFile
View the contents of a file:
cat myFirstFile
View the content of a file with pager (one screenful at a time):
less myFirstFile
View the first several lines of a file:
head myFirstFile
View the last several lines of a file:
tail myFirstFile
Edit a file:
vi myFirstFile
See what files are in your current working directory:
ls
Create an empty directory called myFirstDirectory
:
mkdir myFirstDirectory
Create multi path directory: (creates two directories, src and myFirstDirectory)
mkdir -p src/myFirstDirectory
Move the file into the directory:
mv myFirstFile myFirstDirectory/
You can also rename the file:
user@linux-computer:~$ mv myFirstFile secondFileName
Change the current working directory to myFirstDirectory
:
cd myFirstDirectory
Delete a file:
rm myFirstFile
Move into the parent directory (which is represented as ..
):
cd ..
Delete an empty directory:
rmdir myFirstDirectory
Delete a non-empty directory (i.e. contains files and/or other directories):
rm -rf myFirstDirectory
Make note that when deleting directories, that you delete ./
not /
that will wipe your whole filesystem.