New Update Launched Contact Us Download Now!

Common Linux Commands Every Developer Should Know

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

These are some basic and commonly used Linux commands by developers and engineering students. Here is a cheatsheet with all Linux commands:

1)pwd 

-Display the current working directory

Examples of pwd with different flags:

pwd  :Display the current working directory. Example: /home/foobar

pwd -P :Display the current working directory physical path without any symbolic link name. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/realdir

pwd -L :Display the current working directory logical path - with a symbolic link name, if any. Example: If standing in a dir /home/symlinked, that is a symlink to /home/realdir, this would show /home/symlinked

2)cd

- also known as chdir (change directory)

- it is used to change the current directory

- The cd command can be used to change into a subdirectory, move back into the parent directory, move all the way back to the root directory, or move to any given directory.

Examples:

user@datavoid:~$ cd games

user@datavoid:~/games$

3)ls

- it is a command to list computer files and directories.

- it can be used with different options. 

Some Options are:

-R Recursively list items in subdirectories and subdirectories …

-t sort the list by modification time. (default is alphabetical)

-u sort the list by last access time.

-c sort the list by last attribute (status) change time.

-r reverse the order, for example, the most recent time last.

--full-time to show times with seconds and milliseconds instead of down to the minute.

-1 one entry per line.

-m Stream format; list items across the page, separated by commas.

-g include group but not owner

-o include owner but not group (when combined with -g both group and owner are suppressed)

-d shows information about a directory or symbolic link, rather than the contents of a directory or the link's target.

-F append a "/" to directory names and a "*" to executable files.

Examples:

$ ls -l

drwxr--r--   1 fjones editors     4096 Mar  2 12:52  drafts

-rw-r--r--   3 fjones editors    30405 Mar  2 12:52  edition-32

-r-xr-xr-x   1 fjones bookkeepers 8460 Jan 16  2022  edit.sh

4)mkdir

 - is used to make a new directory.

 -Normal usage is as straightforward as follows:

mkdir name_of_directory

where name_of_directory is the name of the directory one wants to create. When typed as above (i.e. normal usage), the new directory would be created within the current directory.

Some Options are:

-p (--parents): parents or path, will also create all directories leading up to the given directory that do not exist already. For example, mkdir -p a/b will create directory a if it doesn't exist, then will create directory b inside directory a. If the given directory already exists, ignore the error.

-m (--mode): mode, specify the octal permissions of directories created by mkdir .

-p is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts.

Examples:

An example of -p in action is:

mkdir -p /tmp/a/b/c

If /tmp/a exists but /tmp/a/b does not, mkdir will create /tmp/a/b before creating /tmp/a/b/c.

And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself)

5)rmdir

-is used to remove an empty directory.

- Normal usage is straightforward:

 rmdir name_of_directory

where name_of_directory corresponds with the name of the directory one wishes to delete. There are options to this command such as -p in Unix which removes parent directories if they are also empty.

For example:

 rmdir -p foo/bar/baz

will first remove baz/, then bar/ and finally foo/ thus removing the entire directory tree specified in the command argument.

rmdir will not remove a directory if it is not empty in UNIX.

6)rm

The rm command will remove a directory and all its contents recursively. 

For example:

 rm -r foo/bar/baz

 rm -rf foo/bar/baz

7)cp

- for copying files and directories.

- cp has three principal modes of operation. These modes are inferred from the type and count of arguments presented to the program upon invocation.

When the program has two arguments of path names to files, the program copies the contents of the first file to the second file, creating the second file if necessary.

When the program has one or more arguments of path names of files and following those an argument of a path to a directory, then the program copies each source file to the destination directory, creating any files not already existing.

When the program's arguments are the path names to two directories, cp copies all files in the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option flag, typically r, to indicate the recursive copying of directories. If the destination directory already exists, the source is copied into the destination, while a new directory is created if the destination does not exist.

Options:

-f (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command.

-H (dereference) – makes the cp command follows symbolic links (symlinks) so that the destination has the target file rather than a symlink to the target.

-i (interactive) – prompts with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If one enters y (or the locale's equivalent of y), the cp command continues. Any other answer prevents the cp command from overwriting the file.

-n (no clobbering) – prevents accidentally overwriting any files

-p (preserve) – the -p flag preserves the following characteristics of each source path in the corresponding target: the time of the last data modification and the time of the last access, the ownership (only if it has permission to do this), and the file permission-bits.

-R or -r (recursive) – copy directories recursively

Examples:

Creating a copy of a file in the current directory:

cp prog.c prog.bak

This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c file.

Copy two files in the current directory into another directory:

cp jones smith /home/nick/clients

This copies the files jones to /home/nick/clients/jones and smith to /home/nick/clients/smith.

Copy a file to a new file and preserve the modification date, time, and access control list associated with the source file:

cp -p smith smith.jr

This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file's access control protection.

Copy a directory, including all its files and subdirectories, to another directory:

cp -R /home/nick/clients /home/nick/customers

This copies the directory clients, including all its files, subdirectories, and the files in those subdirectories, to the directory customers/clients.

8)mv

- moves one or more files or directories from one place to another.

Options:

-i interactively process, write a prompt to standard error before moving a file that would overwrite an existing file. If the response from the standard input begins with the character 'y' or 'Y', the move is attempted. (Overrides previous -f option.)

-f force overwriting the destination (overrides previous -i option).

Examples:

mv myfile mynewfilename

- renames 'myfile' to 'mynewfilename'.

mv myfile subdir/myfile

- moves 'myfile' to 'subdir/myfile' relative to the current directory

mv myfile subdir

- same as the previous command, the filename is implied to be the same

mv be.03 /mnt/bkup/bes

- copies 'be.03' to the 'bes' directory of the mounted volume 'bkup', then 'be.03' is removed

mv be.03/* /mnt/bkup/bes

-Same as above, except each file moved out of be.03 is deleted individually instead of all being deleted at once after the entire copying is finished.

mv afile another mydir

- moves multiple files to the directory 'mydir'

mv /var/log/*z ~/logs

- This takes longer than expected if '/var' is on a different file system, as it frequently is, since files will be copied and deleted. The shell expands ~ to the user's home directory and treats * as a wildcard character.

9)rename

- The rename command is used to rename files. It is useful for renaming a large group of files.

Syntax:

rename 's/old-name/new-name/' files

For example, to convert all the text files into pdf files, execute the below command:

rename 's/\.txt$/\.pdf/' *.txt  

10)man

-The man command displays the manual pages for a specified command.

Options: None

Example: "man ls" displays the manual pages for the ls command.

11)fork 

-The fork command creates a new process by duplicating the calling process.

Options: None

Example: "pid_t pid = fork();" creates a new process by duplicating the calling process and assigns the child process ID to the variable "pid".

12)exec

-The exec command replaces the current process image with a new process image specified in the command line.

Options: Various options depending on the specific exec function being used (e.g., execl, execv, execvp).

Example: "execvp("ls", argv);" replaces the current process image with the "ls" command, with arguments specified in the array "argv".

13)getpid

The getpid function returns the process ID of the calling process.

Options: None

Example: "pid_t pid = getpid();" assigns the process ID of the calling process to the variable "pid".

14)exit

-The exit function terminates the calling process and returns a status value to the parent process.

Options: A status value, typically an integer.

Example: "exit(0);" terminates the calling process and returns a status value of 0 to the parent process.

15)wait 

- The wait function waits for a child process to terminate and retrieves the child's exit status.

Options: None

Example: "wait(NULL);" waits for a child process to terminate and retrieves the child's exit status, discarding the value.

16)close 

-The close function is used to close a file descriptor.

Options: None

Example: "close(fd);" closes the file descriptor "fd".

17)stat 

-The stat function returns information about a file, such as its size, permissions, and modification time.

Options: None

Example: "stat("file.txt", &buf);" returns information about the file "file.txt" and stores it in the "buf" structure.

18)opendir 

-The opendir function is used to open a directory stream and return a pointer to the directory.

Options: None

Example: "DIR *dir = opendir("/path/to/dir");" opens the directory "/path/to/dir" and returns a pointer to it, which is stored in the "dir" variable.

19)readdir 

-The readdir function is used to read the next directory entry in a directory stream.

Options: None

Example: "struct dirent *entry = readdir(dir);" reads the next directory entry in the directory stream pointed to by "dir" and stores a pointer to it in the "entry" variable.

20)clear 

-The clear command is used to clear the terminal screen.

Options: None

Example: "clear" clears the terminal screen.

21)grep

-The grep command is used to search for a pattern in a file or input.

Options: Various options such as "-i" for case-insensitive search, "-v" for inverted search, etc.

Example: "grep -i "error" log.txt" searches for the pattern "error" in the file "log.txt" in a case-insensitive manner.

22)wc 

-The wc command is used to count the number of lines, words, and characters in a file or input.

Options: Various options such as "-l" for line count, "-w" for word count, etc.

Example: "wc -l file.txt" counts the number of lines in the file "file.txt".

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.