Bash File Manipulation Commands
Table of Contents
Bash File Manipulation Commands
These are common Bash commands used to create, remove, copy, move, and rename files and directories in the command-line interface.
touch: Create a File or Update a Timestamp
The touch command is primarily used to create a new, empty file. If the file already exists, touch will update its access and modification timestamps to the current time without changing its contents.
Syntax: touch [OPTIONS]… FILE…
Common Options: * -c, --no-create: Prevents touch from creating a new file if it doesn't exist. * -a: Change only the access time. * -m: Change only the modification time.
Examples: * Create a single empty file:
```bash touch report.txt ```
Create multiple empty files at once:
touch chapter1.md chapter2.md chapter3.md
Update the timestamp of an existing file:
touch existing_file.log
mkdir: Create a Directory
The mkdir command is used to create one or more new directories.
Syntax: mkdir [OPTIONS]… DIRECTORY…
Common Options: * -p, --parents: Create parent directories as needed. If you try to create a directory inside a non-existent directory, mkdir will create the necessary parent directories first. It won't return an error if the directory already exists. * -v, --verbose: Prints a message for each directory created.
Examples: * Create a single directory in the current location:
```bash mkdir project_files ```
Create a nested directory structure:
mkdir -p project/assets/images
rm: Remove Files or irectories
The rm command is used to delete files and directories. Use this command with caution, as deletions are permanent and cannot be easily undone.
Syntax: rm [OPTIONS]… FILE…
Common Options: * -i: Interactive mode. Prompts for confirmation before each removal. * -f, --force: Force removal. Ignores nonexistent files and never prompts for confirmation. * -r, -R, --recursive: Removes directories and their contents recursively. This option is required to delete a directory.
Examples: * Delete a single file:
```bash rm temp_file.tmp ```
Delete a directory and all of its contents (use with extreme caution):
rm -r old_project/
Forcefully delete a directory and its contents without prompting:
rm -rf obsolete_data/
cp: Copy Files or irectories
The cp command is used to copy files or directories from a source to a destination.
Syntax: cp [OPTIONS]… SOURCE DESTINATION
Common Options: * -r, -R, --recursive: Copy directories and their contents recursively. This is required to copy a directory. * -i: Interactive mode. Prompts for confirmation before overwriting an existing file. * -v, --verbose: Explains what is being done.
Examples: * Copy a file to the same directory with a new name:
```bash cp config.yml config.backup.yml ```
Copy a file to another directory:
cp image.jpg ./backups/
Copy an entire directory to a new location:
cp -r /var/www/website ./website_backup
mv: Move or Rename Files or irectories
The mv command is used to either move files/directories to a new location or to rename them. The command's behavior depends on the last argument. If the destination is an existing directory, it moves the source file/directory into it. If the destination is a new name, it renames the source.
Syntax: mv [OPTIONS]… SOURCE DESTINATION
Common Options: * -i: Interactive mode. Prompts for confirmation before overwriting an existing file. * -n, --no-clobber: Does not overwrite an existing file. * -v, --verbose: Explains what is being done.
Examples: * Rename a file:
```bash mv old_filename.txt new_filename.txt ```
Move a file into a different directory:
mv document.pdf ./archives/
Move a directory:
mv ./temp_files /var/tmp/