Home Linux File Manipulation in Linux/Unix

File Manipulation in Linux/Unix

by Anup Maurya
24 minutes read

Working with files and directories is a fundamental aspect of using the Linux operating system. Whether you’re a newcomer or a beginner, understanding basic file manipulation commands can significantly enhance your Linux experience. In this tutorial, we’ll walk you through essential commands for creating, copying, moving, and removing files and directories. Let’s get started!

Making a Directory

To create a new directory (folder) in Linux, use the mkdir command followed by the desired directory name. For example:

mkdir my_directory

This will create a new directory named “my_directory” in your current location.

Removing a Directory

To remove an empty directory, employ the rmdir command:

rmdir my_directory

Make sure the directory is empty before attempting to remove it using this command.

Creating a Blank File

Creating an empty file is simple with the touch command:

touch my_file.txt

This command generates a new text file named “my_file.txt.”

Copying a File or Directory

The cp command allows you to copy files or directories. For instance, to copy a file named “source.txt” to a new file “destination.txt”:

cp source.txt destination.txt

To copy a directory and its contents to a new location:

cp -r source_directory/ destination_directory/

Moving a File or Directory

Moving (renaming) a file or directory can be done with the mv command. To rename a file “old_name.txt” to “new_name.txt”:

mv old_name.txt new_name.txt

For moving a file or directory to a different location:

mv file_or_directory destination_directory/

Removing a File

To remove a file, use the rm command:

rm my_file.txt

Be cautious when using the rm command, as it permanently deletes files.

One Final Note

When dealing with file and directory names that contain spaces or special characters, it’s a good practice to enclose the names in quotes:

cp "file name with spaces.txt" destination_directory/

Summary

Congratulations! You’ve learned essential Linux commands for file manipulation:

  • mkdir: Create a directory.
  • rmdir: Remove an empty directory.
  • touch: Create an empty file.
  • cp: Copy files or directories.
  • mv: Move (rename) files or directories.
  • rm: Remove files.

Remember, practice is key to mastering these commands. Feel free to experiment in your Linux terminal to become more comfortable with file manipulation. As you delve deeper into Linux, you’ll find these skills invaluable for managing your files and organizing your system efficiently.

related posts

Leave a Comment