Ubuntu Linux: How to create, copy, move or delete files and directories?

In this post I will show you how to create, copy, move and remove files and folders at Ubuntu Linux operating system.

Creating files and directories

Usually files are created by specific programs, but if need create empty files you need use touch command and file name as a parameter:

touch file_name

You can create a directory using mkdir command with the directory name as a parameter:

mkdir directory_name

Remember that you cannot create two directories with same name at the same location. If you try do this, you get a following error message:

mkdir: cannot create directory 'directory_name': File exists

Let’s look at the some parameters for the mkdir command:

  • -m – this option allow us set the files mode e.g.permissions
  • -v, –verbose – information if directory was created
  • -p – allow us creating the parent folders

e.g.: creating directory with name folder1, permissions 111 (execute option for owner, group and others) and displaying information if directory was created

mkdir -m 111 -v folder1

Copy command

At Ubuntu Linux operating system coping files and directories are run with the same command – cp. You need to put to the command the name of the file or folder and the destination directory. It also posible to copy files or folders with different name at the destination folder, e.g.:

cp file1 directory1/file2

The above command copies file1 to directory1 and changes the name of the file1 to file2 at the destination folder. 

Let’s look at the some parameters for the cp command:

  • -b, –backup – if you copy the file which exists at the destination directory, you can create a backup of the previous file
  • -f, –force,  – if you copy the file which exists at the destination directory, it will be overwritten without notice
  • -i, –interactive – if you copy the file which exists at the destination directory, system will ask you if you want to replace the existing file (y means yes)
  • -v, –verbose – the information if directory or files was copied

Moving files or directories

For moving files or directories you also need know only one command – mv. This command also help you at changing files or directories name, e.g.:

mv file1 file2

The above command changes the name of file1 to file2. Remember that if file1 and file2 exists, you can lost the content of file2. 

The parameters for the mv command are very similar like for cp command.

Removing files and directories

For removing files, you need rm command, but for directories – rmdir. However, running rmdir command only allows you to delete empty directories. Deleting non-empty directories requires the use of the rm command with the -r parameter, which allow us for recursive removal.

Let’s look at the below parameter for rmdir command:

  • –ignore-fail-on-non-empty – we won’t be informed of an attempt to remove a non-empty directory
  • –verbose – we will be informed about removing the directory or not if it was not empty

For the rm command we have the following parameters:

  • -r, -R, –recursive
  • -f, –force
  • -i, –interactive
  • -v, –verbose

If you enjoyed this post please add the comment below or share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu