Creating and Managing Files and Directories | mkdir touch cp mv rm
mkdir, touch, cp, mv, rm, rmdir, Linux file management commands
Creating and Managing Files and Directories
File and directory management is at the heart of daily Linux terminal work. Whether you are setting up a project, organizing log files, or deploying an application, these Linux commands give you full control over your file system from the command line.
Creating Directories with mkdir
The mkdir command creates new directories. The -p flag creates parent directories as needed and does not error if the directory already exists, making it safe to use in scripts.
mkdir projects
mkdir -p projects/webapp/src
mkdir -p /tmp/test/{logs,data,config}The brace expansion syntax {logs,data,config} creates multiple subdirectories at once, which is a powerful shortcut for setting up project structures quickly.
Creating Files with touch
The touch command creates an empty file if it does not exist. If the file already exists, touch updates its access and modification timestamps without changing its content. It is commonly used to create placeholder files or update timestamps in build systems.
touch notes.txt
touch file1.txt file2.txt file3.txt
touch /tmp/test/config/app.confCopying Files and Directories with cp
The cp command copies files and directories. The -r flag (recursive) is required when copying directories and their contents. The -p flag preserves the original file's timestamps and permissions.
cp notes.txt notes-backup.txt
cp -r projects/ projects-backup/
cp -rp /var/www/html /tmp/html-backupMoving and Renaming with mv
The mv command moves files or directories to a new location. It is also used for renaming — moving a file to the same directory with a different name.
mv notes.txt Documents/
mv old-name.txt new-name.txt
mv -i source.txt destination.txtThe -i flag prompts for confirmation before overwriting an existing file, which is a good safety practice.
Deleting Files and Directories with rm
The rm command deletes files permanently — there is no recycle bin in the Linux terminal. The -r flag recursively deletes directories. The -f flag forces deletion without prompting. Use these flags carefully.
rm notes.txt
rm -r old-project/
rm -rf /tmp/test/Never run rm -rf / or rm -rf /* — these commands will destroy your entire file system. Always double-check the path before running rm with the -rf flags.
