Ubuntu Terminal Window
Now that you installed Ubuntu, learned how to navigate in Ubuntu using Terminal window, lets take a look on how to manipulate the files and directories in CLI.
1) Creating a file in Ubuntu using Terminal.
There are a few ways. The most standard way to create a file across the linux systems is touch command. So, if you are in your home folder (cd ~), type:
touch myfirstfile
and press Enter. It should have created a file called “myfirstfile in the folder. To check, just type ls and press enter, it should list the files in the folder with myfirstfile in it. Please note, you must have sufficient permissions to create the file. If your user does not have the permissions, try sudo touch myfirstfile instead.
The second way to create a file is the Ubuntu GUI way:
gedit myfirstfile or sudo gedit myfirstfile
This should open up a default Ubuntu GUI text editor window – gedit, which is a pretty cool editor and I will probably have another post on how to use it appropriately. You will probably like this way to edit the files the most if you are just starting with Ubuntu.
There are other ways:
vi and vim are the common Linux CLI text editors. If you have these installed, you can try:
vim myfirstfile or vi myfirstfile appropriately, also, may have to do sudo vim myfirstfile or sudo vi myfirstfile if needed. I personally like vi since I have been using it before I started with Ubuntu, but it is a bit complicated to start with. I may need to write another post on how to use it and list the shortcuts.
Anyways, for all we will do from now, touch or gedit will do just fine.
2) Create new directories – mkdir command (short for make directory).
Lets try creating new directory. Make sure you are in your user directory – (cd ~ or just cd) and type:
mkdir myfirstdirectory
Please note, depending on your user, you may have to type sudo before every command, so if some of the actions do not work, try sudo mrdir myfirstdirectory which will execute the command you need as a superuser.
Now type ls or ls -l and you should see the newly created directory.
3) copy the files and directories – cp command(short for copy).
cp for copy, here is how to copy a file… Make sure you are in your home directory and have created a file myfirstfile as described in step 1 of this post. Now type:
cp myfirstfile myfirstfilecopy
this would copy myfirstfile and make a copy in the same directory, calling it myfirstfilecopy.
Now make sure you have a directory created in step 2 and located in the home folder. Lets copy a file to the directory:
cp myfirstfile myfirstdirectory/myfirstfile
This would copy the file into the directory and keep the name. Obviously, there are a few ways to accomplish it, such as:
cp myfirstfile ~/myfirstdirectory/myfirstfile
cp myfirstfile /user/userfoldername/myfirstdirectory/myfirstfile where userfoldername is the name of your user.
We described a few ways of calling the same directory in a previous post – how to navigate in Ubuntu.
Now it is a bit different to copy the directory. You need to add an option -R to copy recursively, meaning including all the files in it. So create a new directory called myseconddirectory. Lets copy myfirstdirectory into myseconddirectory. Type:
cp -R ~/myfirstdirectory ~/myseconddirectory
It is close to copying regular files, just don’t forget the -R flag.
3) Move and rename files and directories – mv command (short for move). Moving the files and directories is very close to copy command. To move files try:
mv myfirstfile myfirstfilemoved
This will move “myfirstfile” file into the same folder and as you can see, it can be used for renaming the files as well. The file is now called “myfirstfilemoved”.
For the directories it is close, just use the -R flag for recursive.
mv -R myfirstdirectory myseconddirectory/myfirstdirectory
It will move “myfirstdirectory” into the “myseconddirectory” directory. So once you cd myseconddirectory, you will see your “myfirstdfirectory” inside after typing ls. 🙂
4) Delete files and directories using rm command. rm stands for remove.
To remove file just type:
rm myfirstfile
To remove the directory, once again, use -R recursive option:
rm myseconddirectory
Seems easy? Practice a bit, but be careful. This is Linux, no confirmations, as soon as you do something, it is done. Make your own files to practice with, don’t touch the ones that are already there. Eventually you will learn what the files and directories in Linux are kind of the same throughout different distributions. It is kind of like Windows – remember when you first started to find out that my computer is the place you need to go to… 🙂
Please leave us the comments if you find this post useful or have any questions or suggestions. We will be slowly moving along and making out how to posts more complicated, referencing the older posts as we go. So if you find any problems with this post, let us know as well.