Creating and deleting files and directories are standard operations for a sysadmin. Depending on your operating system and filesystem, there may be different ways to perform these tasks. The most efficient way is to use the shell (for instance, Bash). This article assumes you already understand how to enter commands into a Linux terminal. (Read Nathan Lager's What sysadmins need to know about using Bash if you want a refresher.)
Connect to your Linux terminal and get ready to sling some files.
[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]
Create a directory
Before creating a new directory, use the pwd
command to understand where you are in the filesystem:
$ pwd
/home/localuser
I'm in the localuser's home folder (and you're probably in whatever user's home directory you've logged in as). Checking for files with the ls
command, I see that I have none:
$ ls
$
Create a new directory as the starting point for this article's exercises. The command to create a new directory is mkdir
. Check its options and available parameters:
$ mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
The basic syntax for using this command is mkdir {dir}
(replace {dir}
with the desired name of your directory). Before creating any directory or file, remember that most Linux filesystems are case-sensitive. That means a resource named Dir
or File
is different from dir
or file
, which is also different from DIR
or FILE
. Bash interprets the name exactly as you spell it.
Create a new directory called mydir
, change to it with the cd
command, and list its contents:
$ mkdir mydir
$ file mydir
mydir: directory
$ ls -l
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:47 mydir
$ cd mydir/
$ pwd
/home/localuser/mydir
$ ls -l
total 0
You've just created a new directory, confirmed that it is indeed a directory-type object with the file
command, entered the directory, and verified that it is empty. To create more than one directory simultaneously, specify the names of the new directories after mkdir
with a blank space between them:
$ mkdir dir1 dir2 dir3
$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
[mydir]$ ls -R
.:
dir1 dir2 dir3
./dir1:
./dir2:
./dir3:
You created three new empty directories.
[ Get insight into managing your Linux environment for success. ]
To create a directory with a directory inside of it, use the -p
option:
$ mkdir -p dir4/subdir1
$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4
$ ls -lR
.:
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4
./dir1:
total 0
./dir2:
total 0
./dir3:
total 0
./dir4:
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1
./dir4/subdir1:
total 0
[mydir]$ ls -l dir4/
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1
Create files
Now that you have some directories, create some files. There are multiple ways to create files. To create files using shell redirection, refer to How to manipulate files with shell redirection and pipelines in Linux. You can also create empty files with the touch
command. Here are its options and parameters:
$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit
The basic syntax to create an empty file is touch {file}
:
$ ls
dir1 dir2 dir3 dir4
$ touch file1
$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1
To create multiple files, type each file's name in front of another with blank spaces separating them:
$ touch file2 file3
$ ls -l
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3
To create an empty file inside a subdirectory, specify the full path of the desired directory before the name of the new file:
$ touch dir4/subdir1/file4
$ ls -lR
.:
total 0
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2
drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3
drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3
./dir1:
total 0
./dir2:
total 0
./dir3:
total 0
./dir4:
total 0
drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:35 subdir1
./dir4/subdir1:
total 0
-rw-rw-r--. 1 localuser localuser 0 Jun 9 17:35 file4
[mydir]$ ls dir4/subdir1/
file4
Delete files and directories
Now that you have created some files and directories, you can delete everything you've created so far.
It can be easy to get disoriented in the terminal, which can have disastrous consequences. Use the pwd
command to display exactly which part of the filesystem you're in.
The safest way to remove files and directories is to send them to a trash bin, just as you do on your desktop. Projects such as trashy
and trash-cli
help you remove files from a directory without actually deleting anything until you're ready for the data to be erased. However, these tools are not often installed by default. The standard tool to remove resources is rm
.
Use the rm
command when you're sure you're ready to erase data permanently. Unlike trash commands, there is no unremove command, so use rm
judiciously.
To delete a file, use rm {file}
:
$ ls dir3/
dir2 file3
$ rm dir3/file3
$ ls dir3/
dir2
To delete a directory and its contents, use the -r
or -R
option with rm
:
$ rm -r dir3/dir2/
$ ls dir3
$
If you're dealing with an empty directory (such as my example dir3
, which had its contents removed), use the -d
parameter to delete it:
$ ls dir3/
$ rm -d dir3/
Advanced operations
The shell makes creating files and directories easy, scriptable, and efficient. You can use special shell operations to create multiple directories at a time. Try mkdir
along with seq
:
$ mkdir dir{1..9}
[ Download the free Linux commands cheat sheet. ]
Wrap up
The Linux terminal is a powerful tool. Now you know how to use it for a few basic file-management tasks. Creating and deleting files and directories are essential tasks. Tasks such as copy and move are equally important, and I'll write about them in my next article.
About the author
Alexon has been working as a Senior Technical Account Manager at Red Hat since 2018, working in the Customer Success organization focusing on Infrastructure and Management, Integration and Automation, Cloud Computing, and Storage Solutions. He is a part of the TAM Practices LATAM team based in São Paulo, Brazil, where his job is partnering with, advocating, trust-advising, and supporting customers in their success goals while making use of the complete portfolio. He also contributes to produce and enhance documentation, knowledge-base articles, blog posts, presentations, webinars, and workshops. He is a member of numerous communities in addition to the Sudoers, like Red Hat Academy and Red Hat Accelerators. When he’s not at work, he enjoys spending quality time with his family (wife, daughter, and cat) and participating in several volunteer jobs.
More like this
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech