After knowing about ls command for listing entries inside directory, we are now moving to creating directory in Linux system. On Linux, we can use mkdir command. Mkdir is short for “make directory”.
What is mkdir
Mkdir is a command for creating directories in Linux system. This command is a built-in command.
Run mkdir command
You can type mkdir directly from your console to use it.
By default, running mkdir without any parameter will create directory under the current directory. Here’s a sample of it :
From screenshot above, we created directory called office. When we run mkdir command, we are in/home/pungki directory. So then the new directory, which is office, is created under /home/pungkidirectory. If we put an exact location – for example : /usr/local – , then Linux will create a directory under /usr/local directory.
When Linux found that the directory which suppose to be created is already exist, then Linux will telling us that Linux can’t cretate it.
Another pre-requisite of creating directory that you must have access to the location where the directory want to be created. When you don’t have it then mkdir will report an error.
Create multiple directories
We can also create multiple directories at the same time. Let say we want to create directories namedubuntu, redhat and slackware. Then the syntax will be like this :
$ mkdir ubuntu redhat slackware
Add directory include its sub-directory
When you want to created a include its sub-directory, you will need to use -p parameter. This parameter will create parent directory first, if mkdir cannot find it. Let say we want to create directory named letter and directory named important under directory letter. Then the syntax will be like this :
$ mkdir -p letter/important
Set access privilege
Using -m parameter, we can also set the access privilege for the new directory on-the-fly. Here’s an example.
$ mkdir -m=r– letter
The above command will create a directory named letter and give access privilege read-only for thedirectory owner, directory group owner and anybody.
Print message a message for each created directory
If we want, we can use -v parameter to do this. Here’s an example.
$ mkdir -v ubuntu redhat slackware
Mkdir command is also one of the basic command that must known for everyone who want to learn Linux. As usual, you can always type man mkdir or mkdir –help to display mkdir manual page and explore it more detail.
8 Pratical Examples of Linux “Touch” Command
In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.
In this article we will cover some useful practical examples of Linux touch command. The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.
Touch Command Options
- -a, change the access time only
- -c, if the file does not exist, do not create it
- -d, update the access and modification times
- -m, change the modification time only
- -r, use the access and modification times of file
- -t, creates a file using a specified time
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called sheena.
# touch sheena
2. How to Create Multiple Files
By using touch command, you can also create more than one single file. For example the following command will create 3 files named, sheena, meena and leena.
# touch sheena meena leena
3. How to Change File Access and Modification Time
To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name.
# touch -a leena
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists.
# touch -c leena
5. How to Change File Modification Time
If you like to change the only modification time of a file called leena, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.
# touch -m leena
6. Explicitly Set the Access and Modification times
You can explicitly set the time using -c and -t option with touch command. The format would be as follows.
# touch -c -t YYDDHHMM leena
For example the following command sets the access and modification date and time to a file leena as 17:30(17:30 p.m.) December 10 of the current year (2012).
# touch -c -t 12101730 leena
Next verify the access and modification time of file leena, with ls -l command.
# ls -l total 2 -rw-r--r--. 1 root root 0 Dec 10 17:30 leena
7. How to Use the time stamp of another File
The following touch command with -r option, will update the time-stamp of file meena with the time-stamp ofleena file. So, both the file holds the same time stamp.
# touch -r leena meena
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.
# touch -t YYMMDDHHMM.SS tecmint
For example the below command touch command with -t option will gives the tecmint file a time stamp of18:30:55 p.m. on December 10, 2012.
# touch -t 201212101830.55 tecmint
We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list, please update us via comment box.
Delete Directory Command in Terminal
You need to use the rmdir utility / command. The rmdir utility removes the directory entry specified by each directory argument, provided it is empty. Arguments are processed in the order given. In order to remove both a parent directory and a subdirectory of that parent, the subdirectory must be specified first so the parent directory is empty when rmdir tries to remove it.
The syntax is:
rmdir [option] dirName
Ubuntu delete directory called /tmp/foo
Open the terminal. Type the following command:
$ rmdir /tmp/foo
To remove foo and bar empty directories, type:
$ rmdir foo bar
Recursive directory removal on Ubuntu
Remove all files and directories including all sub-directories i.e. recursive removal:
$ rm -rf /path/to/directory
$ rm -rf /tmp/foo
Please note that you can also pass -p option to the rmdir command. Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component:
$ rmdir -p /tmp/x/y/z
Deleting directories as a superuser on ubuntu
If directory is owned by root or any other user or if you are getting “access denied/permission denied” message, try:
### Warning: careful with sudo and rm/rmdir command. ### ### Check twice before you hit [enter] key ### sudo rmdir /path/to/dir sudo rm -rf /path/to/dir
After knowing about copy command, the next command which is related is mv command. When you want to move files from one place to another and you don’t want to duplicate it, then mv command is absolutely right for this task.
What is mv command
mv command is a command that similar with cp, but it does not create a copy / duplicate of files or directories. This command is installed by default on your Linux system, in any kind of Linux you are using. Please take a look of some examples using mv command in day-to-day operation.
1. Moving files
The requirement of moving file is the file source location must be different with the files destination location. Here’s an example. To move file_1.txt from current directory to another directory , for example/home/pungki/office, here’s the syntax :
$ mv file_1.txt /home/pungki/office
As we can see, when we move the file_1.txt, the file_1.txt from previous directory is deleted.
2. Moving multiple files
If we want to move multiple files, we put them in one line separated by space.
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office
You can also use pattern if your files have it. For example, to move all files which have .txt extension, we can use this command :
$ mv *.txt /home/pungki/office
3. Moving directory
Different from the copy command, moving directory using mv command is pretty straight forward. To move a directory, you can just to use mv command without any options. Please take a look screenshot below.
4. Renaming files or directory
We also use mv command to rename files and directory. But in order to do so, the destination location must be the same with the source location. Then the file name must be different.
Let say we are inside /home/pungki/Documents folder and we want to rename file_1.txt into file_2.txt. Then the command will be like :
$ mv file_1.txt file_2.txt
If we mention the absolute path, then it will look like this :
$ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt
5. Renaming directory
The above rule is also applied to directory. Take a look this example :
$ mv directory_1/ directory_2/
6. Print what happen
When you are moving or renaming a large number of file / directory, you may want to know does your command works successfully or not without seeing to the destination location. To do this, we can use -voption.
For example we want to move all txt files and want to check it. Then the command will be like this.
$ mv -v *.txt /home/pungki/office
The same way is applied to directory.
7. Using interactive mode
When you are moving file into another location, and there is already exist the same file, then by default mv will overwrite it. No pop-up notification for this. To make a notification for overwriting file, we can use -i option.
Let say we want to move file_1.txt to /home/pungki/office. Meanwhile, file_1.txt is already exist in /home/pungki/office directory.
$ mv -i file_1.txt /home/pungki/office
This notification will aware us about the existence of file_1.txt in the destination location. If we press “y” then the file will be moved, otherwise, it will not.
8. Using update option
While -i are notify us about overwriting files, then -u option will perform update only if the source is newer than destination file. Let’s take a look example below.
We have file_1.txt and file_2.txt with this attributes :
- File_1.txt has 84 bytes file size and it last modified time is 12:00
- File_2.txt has 0 bytes file size and it last modified time is 11:59
We want to move them into /home/pungki/office directory. But in the destination location, we already have file_1.txt and file_2.txt.
We move file_1.txt and file_2.txt from current directory into /home/pungki/office using command :
$ mv -uv *.txt /home/pungki/office
As the result, we see those files are moved. Those file is moved because their last modified time stamp is newer than the files in /home/pungki/office directory.
9. Do not overwrite any existing file
If -i options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Using example on point 8, if we change the option from -u to -n, combine with -v option, then we will see that there are no files moved into /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office
10. Create backup when copying
By default, moving files will overwrite the destination files if there are already exist before. But what happen if you are moving wrong files, and the destination files are already overwritten by the new ones? Is there a way to retrieve the old one? Yes there is. We can use -b option. -b option will make a backup of destination file before it overwritten by the new one. Once again, we will use scenario from point 8 above.
$ mv -bv *.txt /home/pungki/office
As you can see on the screenshot, on the /home/pungki/office directory, we have a file named file_1.txt~ and file_2.txt~ . The tilde sign (~) means that those files are backup. We can see the attribute of them is older than file_1.txt and file_2.txt.
Moving file or directory also one of the basic command in Linux system. As usual you can type man mv or mv –help to display its manual page to explore more detail.
Copying files or directories is one of basic activity in every operating system. Backup activity is basically is creating a copy of files and directories. On Linux system, we can use cp command to do it.
What is copy command
As we mentioned above, cp command is a command to create copy of files and directories. Here’s some samples of cp command that might useful in day-to-day operation
1. Run cp without any options
This is a very basic cp usage. To copy a file name myfile.txt from one location to another location, we can type like this :
$ cp myfile.txt /home/pungki/office
If we don’t type absolute path, it mean that we are copying a file on current directory. From example above,myfile.txt is located in /home/pungki/Documents. We don’t have to type/home/pungki/Documents/myfile.txt to copy myfile.txt if we are in that /home/pungki/Documentsdirectory. While /home/pungki/office is a folder where the file will be copied.
2. Copy multiple files at the same time
To copy multiple file at the same time, we can just put the files behind the copy command which separated by space. Here’s an example :
$ cp file_1.txt file_2.txt file_3.txt /home/pungki/office
3. Copy a directory
Copying a directory is a little bit tricky. You need to add -r or -R option to do it. -r or -R option means recursive. This option is a must whether the directory is empty or not. Here’s an example :
$ cp -r directory_1 /home/pungki/office
One more thing to note is that you need to remove the trailing slash behind the directory name. Otherwise you will have an error message like cp : omitting directory ‘directory_1/’
If you got that error, the directory will not copied to the destination folder.
4. Create hard links to files instead of copying them
Copying file means you must have some space on the storage to store the copied files. Sometimes for any reasons, you may want to create “shortcut” or links to the files instead of copying them. To do this, we can use -l option.
$ cp -l file_4.txt /home/pungki/office
From screenshot above, we see that a hardlink of file_4.txt was copied into /home/pungki/office/file_4.txt. It marked by the same inode, 835386. But please note, hardlinks cannot be created into directories. Let’s take a look an example below.
5. Create symbolic links to files
There is another type of links called softlinks or symbolic links. We use -s option to do this. Here’s a sample command.
$ cp -s /home/pungki/Documents/file_6.txt file_6.txt
Creating symlinks only can be done in current directory. On screenshot above, we want to create symbolic links from source directory – /home/pungki/Documents/file_6.txt to /home/pungki/office. But to create symbolic links, I must inside /home/pungki/office as a destination folder. Once I manage to be there, I can run cp -s command above.
Then when you list the file with detail, you will see that /home/pungki/office/file_6.txt is pointing to the original file. Its marked with arrow sign after the file name.
6. Copy without following symbolic links in Source
To do this, we can use -P option. When cp command found a file with symbolic links, it will copy the as is. Take a look at the sample below.
$ cp -P file_6.txt ./movie
As you can see, the cp command will copy file_6.txt as is. The file type still a symbolic link.
7. Copy with following symbolic links in Source
Now we can do this with -L option. Basically, this is an opposite of -P option above. Here’s the sample.
$ cp -L file_6.txt ./movie
With this option, the copied file is the same file with the source file of file_6.txt. This is known from the file size. The copied file has 50 bytes file size while the file_6.txt as symbolic link has 33 bytes file size.
8. Archive the files
When we are going to copy a directory, we will use -r or -R option. But we can also use -a option to archive file. This will create an exact copy of files and directories including symbolic links if any. Here’s a sample :
$ cp -a directory_1/ /home/pungki/office
The above command will copy a directory named directory_1 into folder /home/pungki/office. As you can see, the file_6.txt still copied as symbolic links.
9. Explain what is being done
By default, when copying activity is success, we will see a command prompt again. If you want to know what happen during the copying file, we can use -v option.
$ cp -v *.txt /home/pungki/office
When we copying all txt files in current directory to /home/pungki/office/ directory, -v option will show what is being done. This additional information will make us more sure about the copying activity.
10. Copy only when the source file is newer
To do this, we can use -u option. Take a look this example below.
$ cp -vu *.txt /home/pungki/office
In the beginning, we see file_1.txt has 0 bytes file size. Then we edit it using vi, add some content and save it. Next, we see the file size has changed into 36 bytes.
Meanwhile in /home/pungki/office directory, we already have all *.txt files. When we use -u option, combine with -v option to see what is being done, cp command will only copy a file(s) which is newer from destination directory. As the result, we see that only file_1.txt is copied into /home/pungki/office directory.
11. Use interactive mode
Interactive mode will ask if the destination folder have already the file. To activate interactive mode, use -ioption.
$ cp -ir directory_1/ /home/pungki/office/
12. Create backup date of each copied file
When the destination folder already have the file, by default cp command will overwrite the same file in the destination directory. Using –backup option, cp command will make a backup of each existing destination file. ../office will refer to /home/pungki/office. Here’s a sample :
$ cp –backup=simple -v *.txt ../office
As we can see, –backup=simple option will create a backup file which marked by a tilde sign (~) at the end of the file. –backup option has some Control, which are :
- none, off : never backups (even if –backup is given)
- numbered, t : make numbered backups
- existing, nil : numbered if numbered backup exist, simple otherwise
- simple, never : always make simple backups
13. Copy only file attributes
Cp command also provide us with –attributes-only option. As we can guess from its name, this option will only copy a file name and its attributes without copying any data. Here’s a sample.
$ cp –attributes-only file_6.txt -v ../office
From screenshot above, the original file_6.txt file has 50 bytes file size. Using –attributes-only option,the copied file will have 0 bytes file size. This is because the content of file is not being copied.
14. Force copying
Using -f option will force the copying activity. If the destination files cannot be opened, then -f will try again.
$ cp -f *.txt -v ../office
15. Remove destination before copy
To do this, we can use –remove-destination option. This option is contrast with -f option above. If the cp command find the same file name on the destination folder, cp command will remove destination file first, the copy the new one. Here’s an example.
$ cp –remove-destination *.txt -v ../office
Cp command is one of basic Linux commands. For those who want to learn Linux, must know this command. Of course you can type man cp or cp –help from your console to display its manual page to explore more detail.