Basic Linux Commands

Basic Linux Commands

Touch Command

The touch command in Linux is used to update the access and modification times of a file or to create an empty file if it does not exist.

Here is the basic syntax of the touch command:

touch [options] file

Some common options for the touch command include:

  • -a: Change only the access time of the file.

  • -c: Do not create the file if it does not exist.

  • -d <date>: Use the specified date instead of the current time.

  • -m: Change only the modification time of the file.

  • -r <file>: Use the access and modification times of the specified file.

For example,

  • To create an empty file called myfile.txt, you can use the following command:
touch <file_name>
  • To update the access and modification times of an existing file called myfile.txt, you can use the following command:
touch <file_name>
  • This command will create ten empty files with the names file1.txt, file2.txt, file3.txt, etc.,
touch file{1..10}.txt
  • To create an empty hidden file, you can use the following command:
touch .file.txt
  • To change only the access time of myfile.txt, you can use the -a option:
touch -a myfile.txt
  • For more information about the touch command, you can consult the touch man page by running the following command:
man touch

Listing Command:

ls : The ls command is used to list the files and directories in a directory. By default, it lists the files and directories in the current working directory.

Here is the basic syntax of the ls command:

ls [options] [files or directories]

Some common options for the ls command include:

  • -a: Include hidden files and directories in the listing.

  • -l: Display the listing in a long format, showing the permissions, owner, group, size, and timestamps for each file and directory.

  • -h: Display file sizes in human-readable format (e.g., 1K, 234M, 2G).

  • -R: Recursively list the contents of directories and their subdirectories.

For example,

  • To list the files and directories in the current working directory, you can use the following command:
ls
  • To list the files and directories in the /etc directory, you can use the following command:
ls /etc
  • To list the files and directories in the current working directory in a long format, showing hidden files as well, you can use the following command:
ls -al
  • To list only directories, you can use the following command:
ls -d
  • To list all the files having .sh extension:
ls *.sh
  • To list the files and directories with index numbers in inodes:
ls -i
  • For more information about the ls command, you can consult the ls man page by running the following command:
man ls

Directory Commands

pwd

The pwd command stands for "print working directory." It is used to display the path of the current working directory.

For example, if the current working directory is /home/user/documents, pwd command will output the following:

/home/user/documents

cd

The cd command stands for "change directory." It is used to change the current working directory in the Linux file system.

Here is the basic syntax of the cd command:

cd [directory]

The directory argument specifies the path of the directory that you want to change to.

For example,

  • To change the current working directory to /home/user/documents, you can use the following command:
cd /home/user/documents
  • To change the current working directory to your home directory, you can use the following command:
cd
  • To change the current working directory to the parent directory of the current working directory, you can use the .. notation:
cd ..
  • To change the current working directory to 2 levels back from the current working directory, you can use the ../.. notation:
cd ../..

mkdir

The mkdir command stands for "make directory." It is used to create new directories in the Linux file system.

Here is the basic syntax of the mkdir command:

mkdir [options] directory

The directory argument specifies the name of the new directory that you want to create.

Some common options for the mkdir command include:

  • -p: Create any necessary parent directories if they do not exist.

  • -m <mode>: Set the permissions mode for the new directory.

  • -v: Print the name of each created directory to the standard output.

For example,

  • To create a new directory called mydir, you can use the following command:
mkdir mydir
  • To create a new directory called mydir and set its permissions to 0750, you can use the following command:
mkdir -m 0750 mydir
  • To create multiple directories at the same time, you can use the following command:
mkdir dir1 dir2 dir3
  • To create myfolder directory in a specific location(/home/user), you can use the following command:
mkdir /home/user/ myfolder
  • To create a nested Directory, you can use the following command:
mkdir -p dir1/dir2/dir3
  • For more information about the mkdir command, you can consult the mkdir man page by running the following command:
man mkdir

Thanks for reading! Hope you find this article helpful.

Niranjana

Did you find this article valuable?

Support Niranjana Koni by becoming a sponsor. Any amount is appreciated!