This set of data is generated by the l option. The “a” option instead also shows the hidden files.
Where [DIRECTORY] is the name of the directory to change to.
The # character indicates the start of the comment, which
lasts for the entire line after it's found.
This command changes the current working.
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-poption:
This command creates a directory structure
a/b/c with intermediate directories "
a" and "
b" if they do not already exist.
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 typingman <command-name>. Try now withman mkdir for example (press theqkey 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 usingmkdir, you can delete a folder usingrmdir:
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 nobinwhen removing files from the command line, and recovering lost files can be hard.
Note: Directories that contain files cannot be removed with "
rmdir". To remove a non-empty directory, use the "
rm" command with the "
-r" option to remove the directory and its contents recursively.
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, likedrwxr-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.
You type chmod followed by a space and a letter:
- "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
This gives us 4 combinations:
- 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-soption 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 fto search only files, or -type lto only search symbolic links.
-nameis case sensitive. use-inameto 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-deleteoption. 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 runcatto 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:
whichwill 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.