Basic commands and utilities

The Unix file system is a hierarchical file system that organizes files and directories into a tree-like structure. At the root of the file system is the root directory, represented by a forward slash (/). All other files and directories are organized under the root directory in a tree-like structure, with directories serving as branches and files as leaves.

In the Unix file system, each file and directory has a unique name and is stored in a specific location within the file system hierarchy. The file system also supports the use of symbolic links, which are special files that point to other files or directories.

In addition to organizing files and directories, the Unix file system also provides access control and permissions, allowing users to specify which users and groups can access specific files and directories. It also supports file attributes, such as timestamps and file ownership, and allows users to manipulate files and directories using commands such as cp, mv, and rm.

The Unix file system is used on many operating systems, including Linux, macOS, and BSD, and is a fundamental component of the Unix operating system.

In the Linux operating system, files are classified into three main types: regular files, directories, and special files.

Regular files are the most common type of file in a Linux system. They can contain text, numbers, or binary data and can be created, modified, and deleted using standard Linux commands.

Directories are special files that contain a list of other files and directories. They are used to organize and group related files and are represented by a forward slash (/) in the file system hierarchy.

Special files include device files, symbolic links, and sockets, and are used to represent devices, network connections, and other system resources.

Linux uses a case-sensitive file naming system, which means that file names are treated as distinct even if they differ only in the case of their letters. For example, “file.txt” and “File.txt” are considered to be two different files in Linux.

Linux also supports the use of wildcard characters in file names. The most commonly used wildcards are the asterisk (*), which matches any number of characters, and the question mark (?), which matches any single character. For example, the command “ls *.txt” would list all files with a “.txt” extension in the current directory.

In Linux, file names can generally include any combination of letters, numbers, and a few special characters, such as hyphens (-) and underscores (_). However, there are a few restrictions on the use of certain characters in file names:

The forward slash (/), backslash (), and null character () cannot be used in file names, as they have special meanings in the Linux file system.

The period (.) is used to separate the base name of a file from its extension, so it cannot be used at the beginning of a file name.

The space character ( ) cannot be used in file names, as it is used to separate arguments on the command line.

Some characters, such as the asterisk (*), question mark (?), and square brackets ([]) have special meanings in certain contexts and should be used with caution in file names.

It is generally a good idea to avoid using characters that have special meanings in the shell, as they can cause problems when working with the command line. It is also a good practice to use descriptive and meaningful file names to help organize and manage your files.

There are several basic commands that are commonly used to work with the Linux file system. Here are a few examples:

Command Description
ls Lists the files and directories in a directory.
cd Changes the current working directory.
pwd Prints the current working directory.
mkdir Creates a new directory.
touch Creates a new empty file or updates the timestamp of an existing file.
cp Copies a file or directory.
mv Moves a file or directory to a new location or renames it.
rm Deletes a file or directory.

These are just a few examples of the many commands available for working with the Linux file system. To learn more about a specific command, you can use the man command to view the manual page for that command. For example, to learn more about the ls command, you can enter man ls at the command prompt.

Command execution in Linux CLI

In Linux, you can execute commands by typing them at the command prompt and pressing the Enter key. The command will be interpreted by the shell (e.g., bash) and executed with the specified arguments and options.

Here is the general syntax for executing a command in Linux:

command [-options] [arguments]

command is the name of the command that you want to execute.

options are optional flags that modify the behavior of the command. Options are usually preceded by a or character.

arguments are additional values or parameters that are passed to the command.

For example, to list the files in the current directory, you can use the ls command:

ls

To display the contents of a file, you can use the cat command followed by the name of the file:

cat file.txt

To copy a file, you can use the cp command followed by the source file and the destination:

cp file1.txt file2.txt

To display the manual page for a command, you can use the man command followed by the name of the command:

man ls

ls command to list files in directory

In Linux, the ls command is used to list the files and directories in a directory. By default, ls lists the files and directories in the current working directory, but you can also use it to list the contents of a different directory by specifying the directory path as an argument.

Here are a few examples of how the ls command can be used:

Command Description
ls Lists the files and directories in the current working directory.
ls / Lists the files and directories in the root directory.
ls /usr/bin Lists the files and directories in the /usr/bin directory.
ls -l Lists the files and directories in long format, including additional information such as file permissions, owner, and size.
ls -a Lists all files, including hidden files (those that start with a period).
ls -t Lists the files and directories sorted by modification time (most recently modified first).

You can also use multiple options together, such as ls -alt to list all files in long format, sorted by modification time. To learn more about the available options for the ls command, you can use the man command to view the manual page for ls, or you can use the ls –help command to view a summary of the available options.

Here are a few examples of how the ls command can be used:

To list the files and directories in the current working directory:

ls

To list the files and directories in the /usr/bin directory:

ls /usr/bin

To list the files and directories in long format (including additional information such as file permissions, owner, and size):

ls -l

To list all files, including hidden files (those that start with a period):

ls -a

To list the files and directories sorted by modification time (most recently modified first):

ls -t

To list all files in long format, sorted by modification time:

ls -alt

cd command in file system

In Linux, the cd command is used to change the current working directory.

To use the cd command, you simply need to specify the path of the directory that you want to change to. For example, to change to the /usr/bin directory, you would enter:

cd /usr/bin

You can also use the cd command to navigate to directories relative to the current working directory. For example, to navigate to the bin subdirectory of the current working directory, you can enter:

cd bin

You can use the cd .. command to move up one level in the directory hierarchy, and the cd command with no arguments will take you to your home directory.

For example, to navigate to the home directory of the user john, you can enter:

cd /home/john

To navigate to the root directory:

cd /

To navigate to a directory that contains spaces in its name:

cd "My Documents"

To learn more about the cd command and see a list of the available options, you can use the man command to view the manual page for cd, or you can use the cd –help command to view a summary of the available options.

pwd command to get present working directory

In Linux, the pwd command is used to print the current working directory. The current working directory is the directory that you are currently in, and it is represented by the $PWD environment variable.

To use the pwd command, simply enter it at the command prompt:

pwd

This will print the full path of the current working directory to the terminal.

Here is an example of using the pwd command:

$ pwd

/home/john

In this example, the pwd command was run from the user john’s home directory, so it printed /home/john as the output.

You can also use the pwd command in combination with other commands, such as cd to change the current working directory and ls to list the contents of the current working directory.

For example:

pwd && cd /usr/bin && ls

This would print the current working directory, change to the /usr/bin directory, and then list the contents of that directory.

mkdir command in file system

In Linux, the mkdir command is used to create a new directory. To use the mkdir command, you simply need to specify the name of the new directory as an argument. For example, to create a new directory called mydir, you would enter:

mkdir mydir

By default, the mkdir command will create the new directory in the current working directory. You can also specify a different directory path as an argument to create the new directory in a different location. For example:

mkdir /home/john/mydir

This would create the mydir directory in the /home/john directory.

You can also use the -p option with mkdir to create multiple directories at once, even if some of the parent directories do not yet exist. For example:

mkdir -p a/b/c/d

This would create the a, b, c, and d directories, even if the a and b directories did not already exist.

rmdir command to remove directories

In Linux, the rmdir command is used to delete empty directories. To use the rmdir command, you simply need to specify the name of the empty directory that you want to delete as an argument. For example, to delete an empty directory called mydir from the current working directory, you would enter:

rmdir mydir

If the specified directory is not empty, the rmdir command will print an error message and will not delete the directory. To delete a directory and its contents, you can use the rm command with the -r option to delete the directory recursively.

For example:

rm -r mydir

This would delete the mydir directory and all of its contents.

cp command to copy files

In Linux, the cp command is used to copy files and directories. To use the cp command, you need to specify the source file or directory as the first argument and the destination as the second argument. For example, to copy a file called myfile.txt from the current working directory to the /tmp directory, you would enter:

cp myfile.txt /tmp

By default, the cp command will overwrite the destination file or directory if it already exists. You can use the -n option to prevent the cp command from overwriting existing files.

You can also use the -r option with cp to copy directories and their contents recursively. For example:

cp -r mydir /tmp

This would copy the mydir directory and all of its contents to the /tmp directory.

mv command to move or rename files

In Linux, the mv command is used to move files and directories to a new location or to rename them. To use the mv command, you need to specify the source file or directory as the first argument and the destination as the second argument. For example, to move a file called myfile.txt from the current working directory to the /tmp directory, you would enter:

mv myfile.txt /tmp

By default, the mv command will overwrite the destination file or directory if it already exists. You can use the -n option to prevent the mv command from overwriting existing files.

You can also use the mv command to rename a file or directory by specifying the same directory as the source and destination. For example:

mv myfile.txt myfile2.txt

This would rename the file myfile.txt to myfile2.txt.

rm command in filesystem

In Linux, the rm command is used to delete files and directories. To use the rm command, you simply need to specify the name of the file or directory that you want to delete as an argument. For example, to delete a file called myfile.txt from the current working directory, you would enter:

rm myfile.txt

By default, the rm command will delete a file or directory permanently and cannot be undone. Be careful when using the rm command, as there is no recycle bin or trash bin in Linux to recover deleted files.

You can use the -i option with rm to prompt you for confirmation before deleting each file. This can be useful to avoid accidentally deleting important files.

You can also use the -r option with rm to delete directories and their contents recursively. For example:

rm -r mydir

This would delete the mydir directory and all of its contents.

tree command to list files and folders in tree structure

In Linux, the tree command is used to display the directory structure of a file system as a tree. To use the tree command, you simply need to specify the directory that you want to display as an argument. For example, to display the directory structure of the current working directory, you would enter:

tree .

The tree command will display the directory structure in a hierarchical format, with directories shown as branches and files shown as leaves.

Here is an example of the output of the tree command:

.
├── dir1
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
└── dir2
└── file4.txt
2 directories, 4 files

You can use the -d option with tree to show only directories and not files, and the -L option to specify the maximum depth of the directory tree to display.

grep command basic usage for text search

In Linux, the grep command is used to search for patterns in text files. To use the grep command, you need to specify the pattern that you want to search for and the name of the file or files that you want to search in as arguments.

For example, to search for the pattern hello in the file myfile.txt, you would enter:

grep hello myfile.txt

The grep command will search for the specified pattern and print any lines from the file that contain a match to the standard output.

You can use the -i option with grep to ignore case when searching for the pattern, and the -v option to invert the match and print only lines that do not contain the pattern.

You can also use the -r option with grep to search recursively through directories and their contents. For example, to search for the pattern hello in all files in the /usr directory and its subdirectories, you could enter:

grep -r hello /usr

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book