File management in Linux is accomplished through the use of various commands.
Here are some common Linux commands used for file management:
ls
The ls (list) command is used to list the contents of a directory in Linux. The basic syntax for ls is as follows:
Where '[OPTIONS]' are optional flags that modify the behavior of the 'ls' command and '[DIRECTORY]' is the name of the directory to be listed (the current directory by default).
Here are some common options for "ls":
- -a: List all files, including hidden files and directories (those starting with a dot .).
- -l: Display the contents in a long format, showing detailed information about each file or directory.
- -h: Display file sizes in human-readable format (e.g. KB, MB, GB, etc.).
- -r: Reverse the order of the listing.
- -t: Sort the contents by modification time (newest first).
Example usage:
Inside a folder you can list all the files that the folder contains using the ls command:
If you add a folder name or path, it will print that folder's contents:
This command lists the contents of the directory /bin/ in a long format with human-readable file sizes.
"ls" accepts a lot of options. One of my favorite options combinations is -al. Try it:
compared to the plain ls, this returns much more information.
You have, from left to right:
- the file permissions (and if your system supports ACLs, you get an ACL flag as well)
- the number of links to that file
- the owner of the file
- the group of the file
- the file size in bytes
- the file modified datetime
- the file name
This set of data is generated by the l option. The “a” option instead also shows the hidden files.
Note: Hidden
files are files that start with a dot ( . ) |
cd
The cd (change directory) command is used to change the current working directory in Linux. The basic syntax for cd is as follows:
Where [DIRECTORY] is the name of the directory to change to.
Here are some special directory references that can be used with cd:- ".": The current directory.
- "..": The parent directory of the current directory.
- "~": The home directory of the current user.
Example usage:
Once you have a folder, you can move into it using the cd command. “cd” means to change the directory. You invoke it by specifying a folder to move into. You can specify a folder name or an entire path.
Now you are in the "newdir" folder.
You can use the ".." special path to indicate the parent folder:
The # character indicates the start of the comment, which
lasts for the entire line after it's found.
You can use it to form a path:
There is another special path indicator which is “.”, and indicates the current folder. You can also use absolute paths, which start from the root folder “/”:
This command changes the current working.
cp
The cp (copy) command is used to copy files and directories in Linux. The basic syntax for cp is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the cp command, SOURCE is the file or directory to be copied, and DESTINATION is the location to copy the SOURCE to.
Here are some standard options for "cp":
- -a or --archive: Copy the contents of a directory recursively and preserve the original file attributes (such as permissions, timestamps, etc.).
- -r or -R: Recursively copy a directory and its contents.
- -v: Verbose, display progress, and other information during the copy operation.
- -i: Interactive, prompt for confirmation before overwriting an existing file.
Example usage:
You can copy a file using the cp command:
To copy folders you need to add the -r option to recursively copy the whole folder contents:
mv
The mv (move) command is used to move or rename files and directories in Linux. The basic syntax for mv is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the mv command, SOURCE is the file or directory to be moved, and DESTINATION is the location to move the SOURCE to or the new name for the SOURCE if it is being renamed.
Here are some standard options for mv:
- -v: Verbose, display progress, and other information during the move operation.
- -i: Interactive, prompt for confirmation before overwriting an existing file.
- -f: Force move, overwrite existing files without confirmation.
Example usage:
Once you have a file, you can move it around using the "mv" command. You specify the file's current path and its new path:
This is how you rename files and folders. If you have two directories, you want to move, use the following command:
If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved to the folder path identified by the last parameter:
rm
The rm (remove) command is used to delete files or directories in Linux. The basic syntax for rm is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the rm command and [FILE] is the name of the file or directory to be deleted.
Here are some common options for rm:
- -f: Force deletion, ignoring any prompts or errors.
- -r or -R: Recursively delete a directory and its contents.
- -v: Verbose, display progress and other information during deletion.
- --interactive: Prompt for confirmation before deleting each file.
It is important to use "rm" with caution, as once a file or directory is deleted, it cannot be recovered without a backup or data recovery tool.
Example usage:
Once you have a file, you can remove it using the "rm" command:
You can also remove a directory using the "rm" command:
mkdir
The mkdir (make directory) command is used to create new directories in Linux. The basic syntax for mkdir is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the mkdir command and DIRECTORY is the name of the directory to be created.
Here are some common options for mkdir:- -p: Create intermediate directories as necessary.
- -v: Verbose, and display progress and other information during the directory creation.
Example usage:
You create folders using the "mkdir" command:
You can create multiple folders with one command:
You can also create multiple nested folders by adding the -p option:
Options in UNIX commands commonly take this form. You add them right after the command name, and they change how the command behaves. You can often combine multiple options, too.
You can find which options a command supports by typing man <command-name>. Try now with man mkdir for example (press the q key to esc the man page). Man pages are the amazing built-in help for UNIX.
rmdir
The rmdir (remove directory) command is used to remove empty directories in Linux. The basic syntax for rmdir is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the rmdir command and DIRECTORY is the name of the directory to be removed.
Here are some common options for rmdir:- -v: Verbose, display progress, and other information during the directory removal.
Example usage:
Just as you can create a folder using mkdir, you can delete a folder using rmdir :
You can also delete multiple folders at once:
Note:
The folder you delete must be empty |
To delete folders with files in them, we'll use the more generic "rm" command which deletes files and folders, using the "-rf" options:
Be careful as this command does not ask for confirmation and it will immediately remove anything
you ask it to remove.
There is no bin when removing files from the command line, and recovering lost files can be hard.
touch
The touch command is used to create new empty files or update the modification timestamps of existing files in Linux. The basic syntax for touch is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the touch command and FILE is the name of the file to be created or updated.
Here are some common options for touch:
- -a: Only update the access time of the file.
- -m: Only update the modification time of the file.
- -c: Do not create a file if it does not exist (no-create).
Example usage:
You can create an empty file using the touch command:
This command creates a new empty file named "newfile.txt" if it does not already exist, or updates its modification timestamp if it exists.
This command updates the access timestamp of the file "file.txt" without modifying its modification timestamp.
chmod
The chmod (change mode) command is used to change the permissions of files and directories in Linux. The basic syntax for chmod is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the chmod command, MODE is the new permission set for the file or directory, and FILE is the name of the file or directory whose permissions will be changed.
The permissions on a file or directory in Linux can be represented as a three-digit octal number, where each digit represents the permissions for the owner, group, and others, respectively. Each digit is a combination of read (r), write (w), and execute (x) permissions.
For example, rwx means to read, write, and execute permissions, while r-x means to read and execute but not write permission.
The weird strings you see on each file line, like drwxr-xr-x, define the permissions of the file or folder.
Let's dissect it.
The first letter indicates the type of file:
- "-" means it's a normal file
- "d" means it's a directory
- "l" means it's a link
Then you have 3 sets of values:
- The first set represents the permissions of the owner of the file
- The second set represents the permissions of the members of the group the file is associated to
- The third set represents the permissions of everyone else
Those sets are composed of 3 values. rwx means that a specific persona has read, write, and execution access.
Anything that is removed is swapped with a "-", which lets you form various combinations of values and relative permissions: rw-, r--, r-x, and so on.
You can change the permissions given to a file using the chmod command. chmod can be used in 2 ways. The first uses symbolic arguments, and the second is using numeric arguments. Let's start with symbols first, which is more intuitive.
- "a" stands for all
- "u" stands for user
- "g" stands for the group
- "o" stands for others
Then you type either '+' or '-' to add permission or to remove it. Then you enter one or more permissions symbols ( r, w, x ). All are followed by the file or folder name.
Example Usage:
We have a file named "newfile.txt" which has read and write permission for users and groups and only readable permission for others.
To add executable permission for everyone use the following command:
To add read and write to everyone use the following command:
To remove read, write, and Executable permissions from other permissions use the following command:
You can apply the same permissions to multiple permissions by adding multiple letters before the + / - :
In case you are editing a folder, you can apply the permissions to every file contained in that folder using the -r (recursive) flag.
Numeric arguments are faster but I find them hard to remember when you are not using them day to day. You use a digit that represents the permissions of the persona. This number value can be a maximum of 7, and it's calculated in this way:
- 1 if has execution permission
- 2 if has to write permission
- 4 if has read permission
- 0 no permissions
- 1 can execute
- 2 can write
- 3 can write, execute
- 4 can read
- 5 can read, execute
- 6 can read, write
- 7 can read, write and execute
We use them in pairs of 3, to set the permissions of all the 3 groups altogether:
This command adds execute permission for all users for the file "file.sh".
chown
The chown (change owner) command is used to change the owner and/or group of files and directories in Linux. The basic syntax for chown is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the chown command, OWNER is the username or numeric user ID of the new owner of the file or directory, GROUP is the name or numeric group ID of the new group for the file or directory, and FILE is the name of the file or directory whose owner and/or group will be changed.
Here are some standard options for chown:
- -R: Recursively change the owner and/or group of files and directories in a directory and its subdirectories.
Example Usage:
If you have a file that's owned by root, you can't write to it as another user:
You can use chown to transfer the ownership to you:
Files/directories don't just have an owner, they also have a group. Through this command, you can change that simultaneously while you change the owner:
This command changes both the owner and group of the file "testfile.txt" to user and group respectively.
It's rather common to have the need to change the ownership of a directory, and recursively all the files contained, plus all the subdirectories and the files contained in them, too.
You can do so using the -R flag:
You can also just change the group of a file using the chgrp command:
ln
The ln (link) command is used to create hard links or symbolic links in Linux. The basic syntax for creating a hard link is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the ln command, TARGET is the name of the original file or directory, and NEW_LINK is the name of the new hard link to be created.
Here are some standard options for ln:
- -s: Create a symbolic link instead of a hard link.
The ln command is part of the Linux file system commands.
It's used to create links.
What is a link? It's like a pointer to another file. A file that points to another
file. You might be familiar with Windows shortcuts. |
We have 2 types of links:
- Hard links
- Soft links
1. Hard links
Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't
link to external filesystems (disks).
A hard link is created using the following command:
Now any time you edit any of those files, the content will be updated for both.
If you delete the original file, the link will still contain the original file content, as that's not removed until there is one hard link pointing to it.
2. Soft links
Soft links are different. They are more powerful as you can link to other filesystems and to directories, but when the original is removed, the link will be broken. You create soft links using the -s option of "ln" :
Example Usage:
Say you have a file called file.txt. You can create a soft link to it using the following command:
In this case, you can see there's a special “l” flag when you list the file using ls -al, and the file name has a @ at the end, and it's colored differently if you have colors enabled:
Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or
directory" if you try to access it:
find
The "find" command is used to search for files and directories in a specified location. The basic syntax for the "find" command is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the find command, [LOCATION] is the starting directory for the search, and [EXPRESSION] is a set of conditions that specify the files and directories to be found.
Here are some standard options for "find":
- -name: Search for files and directories with a specific name.
- -type: Search for files and directories of a specific type (e.g. f for files, d for directories).
- -mtime: Search for files and directories modified within a certain number of days ago.
- -print: Display the names of the found files and directories.
Example usage:
Find all the files under the current tree that have the " .js" extension and print the relative path of each file matching:
It's important to use quotes around special characters like '*' to avoid the shell interpreting them.
Find directories under the current tree matching the name "src":
Use -type f to search only files, or -type l to only search symbolic links.
-name is case sensitive. use -iname to perform a case-insensitive search.
You can search under multiple root trees:
Find directories under the current tree matching the name "node_modules" or 'public':
You can also exclude a path, using -not -path :
You can search files that have more than 100 characters (bytes) in them:
Search files bigger than 100KB but smaller than 1MB:
Search files edited more than 3 days ago:
Search files edited in the last 24 hours:
You can delete all the files matching a search by adding the -delete option. This deletes all the files
edited in the last 24 hours:
You can execute a command on each result of the search. In this example, we run cat to print the file
content:
notice the terminating " \; ". {} is filled with the file name at execution time.
locate
The 'locate' command is used to find files and directories quickly on a Linux system. It works by searching an index of filenames, which is generated by the updatedb command, rather than by scanning the file system itself. The basic syntax for the locate command is as follows:
Where [OPTIONS] are optional flags that modify the behavior of the locate command, and [PATTERN] is a string that specifies the name of the files and directories to be found.
Here are some standard options for "locate":
- -i: Perform a case-insensitive search.
- -c: Display the number of matching results, but not the results themselves.
Example usage:
If you want to search any file use the following command:
To finds all files and directories named file.txt or FILE.TXT on the system, ignoring the case:
To display, the number of files and directories named file.txt on the system, but not the names themselves.
which
The "which" command is used to determine the location of an executable file in the system's PATH. The basic syntax for the which command is as follows:
Where [COMMAND] is the name of the executable file to be found.
Example usage:
You can do so using 'which'. The command will return the path to the command specified:
which will only work for executables stored on disk, not aliases or built-in shell functions.
whereis
whereis command is used to find the location of the source/binary file of a command and manuals sections for a specified file in a Linux system. If we compare the "whereis" command with the find command they will appear similar to each other as both can be used for the same purposes but the "whereis" command produces the result more accurately by consuming less time comparatively.
Where [OPTIONS] are optional flags that modify the behavior of the locate command.
Here are some standard options for whereis:
- -b : This option is used when we only want to search for binaries.
- -m : This option is used when we only want to search for manual sections.
- -B: This option is used to change or otherwise limit the places where whereis searches for binaries.
- -M: This option is used to change or otherwise limit the places where whereis searches for manual sections.
- -S: This option is used to change or otherwise limit the places where whereis searches for sources.
Example Usage:
To find the location of the source/binary file of a command uses the following command:
To locate the binary of a Linux command let’s say gunzip:
Each of these commands has various options and flags that can be used to customize their behavior, so it is important to consult the manual pages for these commands for more information.