To reiterate, everything in Linux is a file. Files are stored in logical containers (actually, another type of files) called directories. So, we need to know how to work with files and directories. That will be the subject of this article. Wish you nice reading.
Determining the Type of a File
Before working with a file, we need first to determine the type of the file: whether it is a regular text file, binary executable, directory, soft link, block special file, character special file, etc. For this purpose, the file command was written.
Listing the Contents of a Directory
After determining where we are (the current directory) using the pwd command, and changing to a directory (if we need to do) using the cd command, we need to display the contents of a directory. The ls command does this for us.
ls [OPTIONS] [ARGUMENTS]
- To list the contents of the current directory, use ls without arguments.
- To list the contents of the directory called network-scripts:
- To see a long listing of the /etc/ssh directory, use the –l option (small L for long)
- To list the files with human-readable file sizes, use the –h option.
- To list the files, sorted by their last modification date/time (from oldest to newest), use the –ltr option.
- To list all files, use the –a option:
Files and directories whose names start with a dot .are considered hidden; they are not listed by default.
- To list directories recursively, use the –R option.
Using –R causes the ls command to recurse into all sub-directories.
- To display info about the directory itself (not its contents), use the –ld options.
Creating a Directory
To create a directory, use the command mkdir (make directory)
mkdir [DIRNAME] …
- To create a directory named newdata under /opt:
- To create multiple directories with names Jan, Feb, Mar, …, Dec
- To create a directory and create its parent(s) if they don’t exist, use the –p option.
If you try to execute this command without –p, the shell will complain, firing the following error:
Removing an Empty Directory
To remove one or more empty directories (that has no files or sub-directories inside), use the rmdir command.
If you try this command with a non-empty directory, you will get an error:
Removing Any Directory
To remove non-empty (and empty) directories, use the rm –r command.
To enforce deletion without prompting, use the –f option with the rm command.
There are several ways to create files. The one we are going to discuss in this section is using the touch command.
touch [FILENAME] …
- The following will create an empty file /opt/test1.txt
- To create files day00, day01, …, day31:
Copying Files and Directories
To copy one or more files from a given location (source) to another (destination), the cp command is used.
cp FILE1 FILE2
cp FILE1 DESTINATION/
cp FILE1 FILE2 … FILEn DESTINATION/
The first form of the command copies a file from one location to another using a new name for the resulting copy.
The second form copies the file to a destination directory. The new file at the destination directory will have the same name as the original one.
The third form copies several files to a destination directory.
If multiple source files are to be copies at a time, the destination MUST be a directory.
Coping a Directory
To copy a directory, use the same command cp with –r (recursive) option
Moving and Renaming Files and Directories
When copying files and directories, the source file(s) and/or directories remain unaffected. On the other hand, moving files and directories will remove the source(s). You can think of it like copying then removing.
The mv command moves a file or directory from its current location to another. If the
mv FILE1 FILE2 … FILEn DESTINATION/
This will move one or more files from its/their current location to (another) destination directory.
mv OLDNAME NEWNAME
This renames a file or directory.
The rm command is used to remove files.
In this article, we have discussed working with files and directories.
- The file command is used to determine the type of a file.
- The ls command lists the contents of directories.
- The mkdir command creates directories.
- To remove an empty directory, use rmdir command. For non-empty directories, use rm –r
- To create empty files, we can use the touch command.
- The cp command is used to copy files and directories.
- The mv command moves or renames files and directories.
In the next article, we will learn how to obtain help on need. See you there.