In our previous introductory post we emphasized the important roles files play in course of using a Linux system. In this post we would be dealing with the different stuffs we could do with files and directory on our system. Emphasis would be laid on the command-line aspect here because the nautilus file explorer is intuitive enough for normal point and click manipulations of files and direction.
Creating a directory
The mkdir (make directory) command is used to create directories.
The sub-directory new_file is created under the current directory. However, the new directory does not become the current directory. Complete path name can be specified with mkdir.
If you don’t want to create the directory in the current working directory you can simply supply the path to the location where the directory should be created.
Creating a file
Unlike the process of creating directories, there are many ways in which files could be created in Linux. This is because there are different types of files, and these different files need or required different applications for creation – and these applications are used to manipulate them. For instance if you want to create an office documents (PDF, odt, etc.) in a GUI environment, you could use the LibreOffice application. Gimp could be used to create graphics and pictures, text editors (like Gedit, Emacs and Sublime Text) could be used to create text files, and so on. But on the text-based command line, there are also different programs/applications for creating text files and they vary greatly from one program to another.
Using the touch command
It is used by typing touch followed by the name of a file that you want to create, like this;
This command creates an empty file called newfile.txt. Ordinarily, you don’t need to do this to create a file of a particular type, since you’ll use a specialized program to do the job. If you use touch with the name of a file that already exists, touch updates that file’s access and modification time stamps to the current date and time. This can be handy if you’re using a command that works on files based on their access times, and you want the program to treat an old file as if it were new. You might also want to do this if you plan to distribute a collection of files and you want them all to have identical time stamps.
Using the cat command
You can create a file by using the cat command
cat > newFile
After you have typed the contents of the file, you need to press Ctrl+d to end the creation of the file.
cat > myPoem.odt
Enrich the world,
Make it beautiful;
And when you are gone,
Let the earth miss you.
If you have any terminal text editor, you could also use them to create and manipulate text files such as displaying the contents of files on the screen and allow a user to add, delete and change parts of the text. Two common ones that come with almost every Linux distribution are vi and nano. And they both follow a simple and similar syntax.
To create a file using vi you do;
And with NANO you do;
Deleting files and directories
To delete a directory use the rmdir command;
The above command would work if the directory to be deleted is located (as a sub-directory) in the current working directory. While complete pathnames of the directory could also be specified with rmdir.
To delete a file use the rm command. (This command can also be used to delete directories.)
rm file1 file2 file3
The above command will remove the files file1, file2, file3 from your current directory. If the file(s) to be deleted are not located in the current directory, the complete path name has to be given. Like this;
The commonly used options with the rm command include the following;
-i : prompts or alerts you before deleting.
-f : removes a file y force, except when the file does not exit.
-r or -R : deletes a directory along with its sub-directories.
-v : explains what is being done in details.
Copying Files and Directories
cp [options] <source file/s> <destination directory/file>
cp file1 file2
The command above copies the content of file1 into a new one, file2. If file2 already exists, contents will be overwritten by the contents of file1. When the files are not in the current directory, you could also specify full paths across directories.
When the cp command is to be used with copying a directory to another directory, the -r option is used. This ensures that the directory and it sub-directories are copied.
cp -r backup1 backup2
The command copies the backup1 directory and all its files and sub-directories to the backup2 directory. If the directory, backup2, exists, all the contents are added to that directory, otherwise backup2 is created in the current working directory.
-i : alert you before overwriting.
-l : link a file instead of copying it.
-s : creates a symbolic link.
-v : gives details of what is being done.
MOVING AND RENAMING FILES AND DIRECTORY
The mv (move) commands is used to move a file or directory from one location to another. And also this command is used to rename a file or directory. Not that there is a difference between copying and moving, unlike copying moving does not create a new file.
mv programs programfiles
Here, the programs directory is renamed to program_files. But to move a file to another directory you simply do something like this;
mv register /home/alexander/program_files
The file register is moved from the current directory to the /home/alexander/program_files directory.
Some cool options of mv you could use included;
-f : if the file exists at destination, it overwrites without prompting.
-r : interactive, prompts before overwriting at the destination location.
-v : explains what is going on in details.
DISPLAYING CONTENT OF DIRECTORIES
The ls command is used when you want to display the contents of a directory. We have been using this commands severally, so by now you should be familiar with it.
bin cdrom etc initrd.img lost+found mnt proc run srv tmp var
boot dev home lib media opt root sbin sys usr vmlinuz
deja-dup Documents etc Music Pictures tempfile ubuntu
Desktop Downloads examples.desktop nano.save Public Templates Videos
In the above command example, all the files and directories in the alexander directory are listed. If you want to display the files and directories under the current directory, it is optional to specify the directory name with ls (i.e. it is easier for you to just type the ls command alone). But to get a more detailed list of files and directories the l option is used;
ls -l /home/alexander
drwxrwxr-x 2 alexander alexander 4096 May 24 08:46 deja-dup
drwxr-xr-x 10 alexander alexander 4096 Aug 3 09:48 Desktop
drwxr-xr-x 5 alexander alexander 4096 Aug 3 09:22 Documents
drwxr-xr-x 2 alexander alexander 4096 Aug 2 17:33 Downloads
lrwxrwxrwx 1 alexander alexander 28 Jul 30 21:44 etc -> /usr/share/zoneinfo/UTC/etc/
-rw-r–r– 1 alexander alexander 8980 Apr 28 07:46 examples.desktop
drwxr-xr-x 2 alexander alexander 4096 Apr 28 07:54 Music
-rw——- 1 alexander alexander 4 Jul 30 22:28 nano.save
drwxr-xr-x 3 alexander alexander 4096 Jul 30 11:31 Pictures
drwxr-xr-x 2 alexander alexander 4096 Apr 28 07:54 Public
-rw-rw-r– 1 alexander alexander 109 Jul 30 13:34 tempfile
drwxr-xr-x 2 alexander alexander 4096 Apr 28 07:54 Templates
drwxrwxr-x 2 alexander alexander 4096 Jun 6 11:02 ubuntu
drwxr-xr-x 2 alexander alexander 4096 Jul 30 11:30 Videos
Other options to you could try with the ls command include the following;
– a : list all the files and directories including hidden files (a hidden file is one whose name starts with a period or dot).
– F : shows the file type along with the name (‘/’ for a directory, ‘*’ for an executable).
– R : displays the content of the specified directory and all the sub-directories.
– r : displays files and sub-directories in a reverse order.
– S : sorts by the descending order of the file size.
Try and play with these commands for some time and try to explore other areas not covered here and see the beauty of Linux.