LWM-Linux/06 - Linux File Operations/File Creating, Deletion and Renaming.md

2.6 KiB

File Creation, Deleting, and Renaming

1. Creating Files

There are several ways to create files in Linux:

- a. Using touch:

The touch command is the simplest way to create an empty file.

touch filename.txt

This creates an empty file named "filename.txt" in the current directory.

- b. Using redirection:

You can use output redirection to create a file with content.

echo "Hello, World!" > newfile.txt

This creates a file named "newfile.txt" containing the text "Hello, World!".

- c. Using text editors:

You can create and edit files using text editors like nano, vim, or gedit.

nano newfile.txt
vim newfile.txt
gedit newfile.txt

These commands open the respective text editor with a new file named "newfile.txt".

2. Deleting Files

To delete files in Linux, you can use the rm (remove) command:

  • a. Deleting a single file:
rm filename.txt
  • b. Deleting multiple files:
rm file1.txt file2.txt file3.txt
  • c. Deleting files with a specific pattern:
rm *.txt

This removes all files with the .txt extension in the current directory.

  • d. Deleting files interactively (prompts for confirmation):
rm -i filename.txt
  • e. Deleting files forcefully (use with caution):
rm -f filename.txt

Note: Be extremely careful when using rm, especially with wildcards or the -f option, as deleted files cannot be easily recovered.

3. Renaming Files

In Linux, renaming is done using the mv (move) command:

  • a. Basic renaming:
mv oldname.txt newname.txt
  • b. Renaming multiple files using a pattern: To rename multiple files, you can use a loop in the shell. For example, to change the extension of all .txt files to .md:
for file in *.txt; do
    mv "$file" "${file%.txt}.md"
done
  • c. Renaming with a backup:
mv -b oldname.txt newname.txt

This creates a backup of the destination file if it already exists.

  • d. Interactive renaming (prompts before overwriting):
mv -i oldname.txt newname.txt

Additional Tips:

    1. Use tab completion to avoid typos in filenames.
    1. Use the ls command to list files and verify your actions.
    1. Be cautious when using wildcards (*) with rm or mv.
    1. For complex renaming tasks, consider using specialized tools like rename or mmv.
    1. Always double-check your commands, especially when deleting or renaming multiple files.
    1. Consider using version control systems like Git for important files and projects.

Remember that in Linux, file operations are case-sensitive. "File.txt" and "file.txt" are treated as different files.