File Management 101: Basic Commands For New UsersPoated on by
Unix like operating systems (Unix, Linux, MacOS etc), provides a rich set of directory and file management commands that allow users to interact with files and directories efficiently. Here are some commonly used Unix commands for file and directory management:
File Management Commands: Best For Beginners.
If you are new to Unix/Linux, these commands are best for you to get started. These commands are very basic commands that every user must learn.
Practice Commands On Android
Termux on Android is the best Terminal Emulator which allows the users to install and use wide range of Unix/Linux Commands. Download Termux for Android.
It’s important to note that the syntax and available options/arguments can vary depending on the specific Unix command you’re using.
command [options...] arguments...
Let me break it down:
command: This is the name of the Unix command you want to run. It can be a built-in command or an external command (a standalone program or script).
options: These are optional flags or switches that modify the behavior of the command. They are typically preceded by a hyphen (-) or double hyphen (–), and can be used to enable/disable certain features or specify settings.
These optional flags or switches can be marked between Square Brackets .
arguments: These are the inputs or parameters that the command requires to perform its function. They can be files, directories, or other data that the command operates on.
Sometimes the required arguments are marked between angel brackets <>
Here’s an example of a Unix command with options and arguments:
mkdir -p easymux
In this example, mkdir is the command to new directory, –p is an option to ignore error if directory is already exist, and eaysmux is an argument to create the directory.
To get more information about a particular command, you can refer to its manual page by typing man <command>
in the terminal, where <command> is the name of the command you want to learn more about.
In case man is not installed on your system you can use -h or –help option as most commands comes with default help option. For example:
Here is a sort list of File Management Commands
1. pwd Command
The pwd – Present Working Directory is a command that display the path of Current Working Directory.
-P option print the physical directory, without any symbolic links.
Here first result /home/easymux/htdocs is the symbolic link (short cut) of /usr/share/apache2/default-site/htdocs. With option -P you can print the actual path of the directory in case the current directory is a symbolic link.
2. ls Command
The ls – List command display the list of Files and Directories. By default it displays the contents of the Current Directory.
If no argument is passed the ls command will print the contents of current directory otherwise it will print the contents of given directory. See the example in picture.
-a, –all List all including hidden files and directories.
-l This option displays detailed information about files and directories, including permissions, ownership, size, modification time, and more, in a long-format listing.
-s, –size print the allocated size of each file, in blocks.
-h, –human-readable with -l and -s, print sizes like 1K 234M 2G etc.
-F append indicator (one of */=>@|) to entries. e.g. append * to the executable, / to the directory, @ to the symbolic links etc.
-r, –reverse reverse order while sorting.
-S sort by file size, largest first.
-t sort by time, newest first.
-X sort alphabetically by entry extension.
3. cd Command
The cd – Change Directory command Change the shell working directory.
cd [OPTIONS] [DIR]
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
Some symbols used with cd commands:
- . Present directory.
- .. Parent or Back directory of currunt directory.
- – $OLDPWD – The old directory before change to new one.
- ~ $HOME – home directory.
- / Root directory
Note: If no argument is passed cd command will change to $HOME – home directory, in most case /home
-L force symbolic links to be followed if DIR is symbolic link. (Default)
cd -L htdocs
-P force to follow physical directory if DIR is a symbolic link.
cd -P htdocs
Here Physical path of htdocs is /usr/share/apache2/default-site/htdocs and symbolic link of this directory is /home/easymux/htdocs.
The -L option will change to symbolic link home/easymux/htdocs.
The -P option will change to Physical directiry /usr/share/apache2/default-site/htdocs.
4. mkdir Command
The mkdir – Make Directory command creates new directories if not exists.
mkdir [OPTION] DIR
-m, –mode=MODE set file mode (read, write, execute).
mkdir -m=755 programs
-p, –parents This option will hide error if directory is already exists. Also create a parent directory if not exists while creating sub directory.
Let’s consider two condition.
1. In current directory “python” is already exists. And we are try create same name directory.
mkdir -p python
2. In current directory “scripts” directory is not exists and we try to create a sub-directory bash in scripts directory.
mkdir -p scripts/bash
5. touch Command
The touch command update the access and modification times of each FILE to the current time. Also iy create new empty file if file is not exists.
touch [OPTION] FILEs
To create one or more files run the following command.
touch index.html profile.html
6. cat Command
The cat command Concatenate(prints) the contents of one or more files.
It used for concatenate and is used to print the contents of the files which it is given as an argument. It is a very simple but incredibly useful utility, which can be used in a variety of ways.
cat [OPTION] FILEs
You can use cat command for:
- To display tthe contents of a text file.
- To combines the contents of two or more text files
- To create text files quickly.
The most common usage of the cat command is to display the contents of a text file. By simply typing…
In the terminal, the entire contents of the text file “todolist.txt” will be printed in the terminal.
This can be very useful for quickly looking through the contents of a text file without actually having to open the text editor.
-n Option is used to print contents of the text file with line numbers.
cat -n todolist.txt
The cat command can also be used to combine the contents of two or more files. For instance, typing
cat java.txt bash.txt > code-lang.txt
This will create a new text file, code-lang.txt, which will contain the contents of both java.txt and bash.txt. This can be useful to combine multiple text files into a single file.
The cat command can also be used to create a new text file. For instance, if you want to quickly create a new text file with a list of basic commands, you can type
cat > commands.txt
And then simply type the command names in the terminal. When you’re done, press Ctrl+D to signal the cat command that you’re finished, and your new text file will be saved.
Use > to override contents and >> to append contents.
7. cp Command
The cp command stands for “copy,” and it allows you to copy and clone files and directories from one location to another. It is also used for backup.
cp [OPTION] SOURCE DESTINATION
The SOURCE argument specifies the location of the file or directory to copy from, while the DESTINATION argument specifies the location where the copied files should be placed.
[OPTION] are optional and can be used acording to your need.
-f, –force If an existing destination file cannot be opened, remove it and try again.
cp -f data.txt backup/data.txt
-i, –interactive prompt before overwrite (overrides a previous -n option).
cp -i data.txt backup/data.txt
-n, –no-clobber do not overwrite an existing file (overrides a previous -i option)
cp -n data.txt backup/data.txt
-R, -r, –recursive copy directories recursively. (Copies all the files and directories within the specified SOURCE directory.
For example, if you wanted to copy a directory called “mydir” to the “backup” directory, you could use the following command:
cp -r mydir backup
-v, –verbose Prints files that are being copied to DESTINATION file or directory. This option can be used to generate verbose output. This is particularly useful if you are trying debug the command as it display the success and failure message.
cp -v backup/
To copy html directory recursively with verbose in backup directory, type like this…
cp -r -v html backup
8. mv Command
The mv command is used to move or rename files and directories.
mv [OPTION] SOURCE DESTINATION
Rename SOURCE to DESTINATION, or move SOURCE(s) to DESTINATION. This operation depends on if DESTINATION is exists or not, DESTINATION is a DIRECTORY or not.
- If DESTINATION is not exists, SOURCE will be Renamed to DESTINATION.
- If DESTINATION is a file, SOURCE will be Renamed to DESTINATION (SOURCE will removed).
- If DESTINATION is a Directory, SOURCE File or Directory will be Moved to DESTINATION Directory.
For example, if you wanted to rename a file called “file1.txt” you would use the following syntax:
mv file1.txt newfile.txt
This would rename current file file1.txt to newfile.txt.
-f option can be used to force the move to happen, even if a file already exists at the target location. This is useful if you know that you want to overwrite the existing file, for example,
-i option can be used to prompt for confirmation for any existing files. This can help you to avoid overwriting existing files by mistake.
-n option an be used to prevent overwrite to an existing file.
-v Prints files that are being moved or renamed.This option can be used to generate verbose output. This is particularly useful if you are trying debug the command as it display the success and failure message.
9. rmdir Command
The rmdir command removes (delete) the empty directories.
rm [OPTION] DIRECTORY
By default the rmdir command does not remove non-empty directories. You have to first remove files and directories from the directory which you want to remove.
If you try to remove non-empty directory, it will print error message.
Assume we have a directory projects/html. And we want to remove project directory. Then.
–ignore-fail-on-non-empty Do not print error message when failed to remove a non-empty directory.
-p, –parents remove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b’ is similar to ‘rmdir a/b a’
-v, –verbose output a diagnostic for every directory processed.
10. rm Command
The rm command can be used to quickly and easily remove files that are no longer needed.
rm [OPTION] FILES
To remove file report.txt then run…
-f: Deletes a file without prompting you to confirm the action.
rm -f report.txt
-i: Prompts you to confirm the action before deleting the file.
rm -i report.txt
-r: Recursively removes all files and directories within the specified directory. (this time able to remove directory as well)
rm -r subjects/
-v: Verbose output. Displays the name of each file as it is removed.
rm -v file5.txt
Before deleting any files, it’s important to remember to use the -i flag which will prompt you to confirm the action. This is especially important if you are recursively deleting multiple files as it can be easy to accidentally erase something important if you are not careful.