Skip to content
DevOps devops linux 5 min read

Creating, Copying & Moving Files

Almost everything you do as a DevOps engineer on a Linux server involves moving files around: dropping a new config into place, copying a backup, renaming a deploy folder, or clearing out old logs. On a server there is no drag-and-drop file manager, so you do all of this from the terminal. This page covers the core commands for creating, copying, moving, and deleting files and directories on Ubuntu (Ubuntu 22.04 and 24.04 LTS), the most common Linux distribution for servers. Master these six commands and you can manage any file on any server.

Creating an empty file with touch

The touch command creates a new, empty file if it does not already exist. A “file” here just means any document on disk: a config file, a log, a script, anything.

touch notes.txt

Output:

There is no output — Linux is quiet on success, which is normal. You can confirm the file exists with ls -l:

ls -l notes.txt

Output:

-rw-rw-r-- 1 ubuntu ubuntu 0 Jun 15 10:42 notes.txt

The 0 is the file size in bytes — it is empty.

When to use it: touch is for quickly making a placeholder file you will edit later, or for creating multiple empty files at once. If the file already exists, touch simply updates its “last modified” timestamp without changing the contents — handy when a build tool only rebuilds files that have changed.

touch app.log error.log access.log

This creates three files in one command.

Creating directories with mkdir

A directory (the Linux word for a folder) is created with mkdir, short for “make directory”.

mkdir projects

The most useful flag is -p (for “parents”). Normally mkdir fails if a parent folder in the path does not exist. The -p flag creates every missing folder in the path for you, and it never errors if the folder already exists.

mkdir -p /home/ubuntu/projects/webapp/config

Output:

This builds the whole chain — projects, then webapp, then config — in one step. Without -p you would have to create each level by hand.

Tip: Always use mkdir -p in deploy scripts. Because it does not fail when the directory already exists, you can run the same script repeatedly without errors. This property is called “idempotency” (running it once or many times gives the same result).

Copying files and folders with cp

The cp command copies a file. You give it the source first, then the destination.

cp app.conf app.conf.bak

This makes a backup copy called app.conf.bak and leaves the original untouched. Making a .bak copy before editing an important config is a habit worth building.

To copy a file into a different directory:

cp app.conf /etc/myapp/

To copy an entire directory and everything inside it, add -r (“recursive” — meaning it also copies all sub-folders and their files):

cp -r /home/ubuntu/webapp /home/ubuntu/webapp-backup

Output:

Without -r, trying to copy a directory fails with cp: -r not specified; omitting directory.

FlagWhat it doesWhen to use it
-rCopy a directory and its contentsCopying whole folders
-iAsk before overwriting an existing fileInteractive work, to avoid mistakes
-vPrint each file as it is copied (verbose)Watching a large copy progress
-pPreserve timestamps, ownership, permissionsBackups where metadata matters

A safe, informative copy of a folder:

cp -rvp /var/www/site /var/www/site-2026-06-15

Moving and renaming with mv

The mv command (“move”) does two jobs. It moves a file to another location, and — because Linux has no separate “rename” command — it also renames files.

Rename a file (move it to a new name in the same folder):

mv old-name.txt new-name.txt

Move a file into another directory:

mv report.pdf /home/ubuntu/documents/

Move a whole directory — no -r needed, mv handles folders by default:

mv /tmp/build /var/www/release

Gotcha: mv overwrites the destination silently if a file with that name already exists, with no warning. Use mv -i to be prompted first, or mv -n (“no clobber”) to refuse to overwrite. On a production server, this caution saves you from destroying the wrong file.

Deleting files with rm

The rm command (“remove”) deletes files. There is no Recycle Bin or Trash on a Linux server — once a file is removed it is gone.

rm notes.txt

To delete a directory and everything inside it, use -r:

rm -r old-project

The -i flag asks for confirmation on each file, which is a good safety net:

rm -ri logs

Output:

rm: descend into directory 'logs'? y
rm: remove regular file 'logs/access.log'? y

Deleting empty directories with rmdir

rmdir removes a directory only if it is completely empty. It is safer than rm -r because it refuses to delete anything that still contains files.

rmdir empty-folder

If the folder is not empty you get a clear error instead of silent data loss:

Output:

rmdir: failed to remove 'empty-folder': Directory not empty

When to use it: reach for rmdir when you specifically want a guarantee that you are not deleting real data — for example, cleaning up leftover folders that should be empty.

The danger of rm -rf

The single most dangerous command in Linux is rm -rf. The -r deletes recursively, and -f (“force”) removes everything without any prompts or warnings, ignoring errors. Combined, they wipe out entire directory trees instantly and permanently.

WARNING: Never run rm -rf without reading the path twice. A typo like rm -rf / home/ubuntu/cache (note the stray space after /) tells Linux to delete the entire system. Always run ls on a path first to confirm what is there, avoid using rm -rf with variables like rm -rf $DIR/ (an empty $DIR becomes rm -rf /), and never run it as the root user unless you are certain. On a real server there is no undo.

A safer habit is to delete in two steps — list first, then remove:

ls /var/www/old-release
rm -rf /var/www/old-release

Best Practices

  • Use mkdir -p in scripts so they can be re-run safely without errors.
  • Make a .bak copy of any config before editing it: cp nginx.conf nginx.conf.bak.
  • Add -i when working interactively with cp, mv, and rm to catch overwrites and deletions.
  • Prefer rmdir over rm -r when you only mean to remove an empty folder.
  • Always ls a path before deleting it, and read rm -rf commands twice before pressing Enter.
  • Never use rm -rf with an unchecked variable — quote it and confirm it is not empty first.
  • Use sudo only when a file is owned by another user or lives in a system path like /etc or /var, and understand exactly what you are changing.
Last updated June 15, 2026
Was this helpful?