Monday, 22 February 2021

Linux command List

 Let's see the list of Linux commands which are commonly used. This list is helpful for beginners and professionals both.

CommandsDescription
aliasConverts complex commands into simpler ones.
bzip2 / bunzip2Compresses a file / Decompress a bzip2 file.
bzcat / bzmoreDisplays files compressed with bzip2.
calDisplays calendar.
catIt is a multi-function command.
cdChanges the current working directory.
chageSets an expiration date for a user account.
chgrpChanges group.
chmodChanges permission for a file or directory.
chownChanges the owner of a file or directory.
chshChanges the shell.
clearClears the terminal.
commCompares two streams or files.
cpCopies file content from one file to another file.
cutUsed to display the desired column from a file.
dateDisplays the current date.
dfChecks the disk space in the system.
echoPrints the typed word on the terminal.
exitExits from the current user group to the last group.
exportExports shell variables to other shells.
fileDisplays the type of file.
findFinds files for a particular search.
gpasswdTransfers group membership to another user.
grepFilters lines of text containing a certain string.
groupaddCreates a group.
groupdelPermanently removes a group.
groupmodChanges group name.
groupsDisplays the group name to which the current user belongs to.
gzip / gunzipCompresses a file / Decompress a gzip file.
headDisplays the first ten lines of a file.
historyDisplays older commands from the shell command history.
HISTSIZEDetermines the number of commands to be stored in the current environment.
HISTFILEDisplays the file that contains the history.
HISTFILESIZESets the number of commands kept in the history file.
idTells about the user's id in the system.
lessDisplays file content according to the width of the terminal.
locateSearches a file in the database.
lsLists all the files of a directory.
manDisplays the manual page for the specified command.
mkdirCreates directory.
moreDisplays one output screen at a time.
mvRenames directories or files.
odDisplays a file content in octal format.
passwdSet a password for a user group.
pwdDisplay the current working directory location.
PS1Change the prompt name in the terminal.
renameRenames more than one file at once.
rmRemoves a file.
rmdirRemoves a directory.
set -o noclobberPrevents file from getting overwritten.
set +o noclobberAllows overwriting in the existing file.
set -uDisplays undefined variables as an error.
set +uDisplays nothing for an undefined variable.
set -xDisplays shell expansion.
set +xDisables shell expansion.
sedPerforms editing in streams.
sleepWaits for the specified number of seconds.
sortsorts the content in alphabetical order.
suIt allows a user to run a shell as another user.
sudoIt allows a user to start a program with the credentials of another user.
tacDisplays file content in the opposite order.
tailDisplays the last ten lines of a file.
tarCompresses a directory.
teePuts stdin on stdout and then into a file.
timeDisplays time taken to execute a command.
touchIt creates an empty file.
trTranslates characters.
typeDisplays information about command type.
uniqSorts and display multi times repeating lines only once.
unsetRemoves a variable from a shell.
useraddAdds users.
userdelDeletes users.
usermodModifies the properties of a user.
viOpens vi editor to write a program.
wDisplays who is logged on and what are they doing.
wcCounts words, lines, and characters.
whoTells who is logged on the system.
whoamiTells the name of the user.
who am iDisplays the line pointing to your current session.
zcat / zmoreViews the files compressed with gzip.

Linux export Command

 The export command is a built-in utility of Linux Bash shell. It is used to ensure the environment variables and functions to be passed to child processes. It does not affect the existing environment variable.

Environment variables are set when we open a new shell session. At any time, if we change any variable value, the shell has no way to select that change. The export command allows us to update the current session about the changes that have been made to the exported variable. We do not need to wait to start a new shell session.

Syntax:

  1. export [-f] [-n] [name[=value] ...] or export -p  

Let's have a look at various examples of the export command:

Example1: The export command without any argument

The basic export command will display all the exported environment variables of your system. It is executed as follows:

  1. export  

Consider the below snap of output:

Linux export Command

Example2: Display all exported variable on current shell

To display all the exported environment variable of the current shell, execute the command with -p option as follows:

  1. export -p  

Consider the below snap of output:

Linux export Command

Example3: Using export with functions

To use a function with the export command, use the -f option. If we do not use this option, it will be considered as a variable, not function.

Syntax:

  1. export -f function_name  

We are exporting a function 'name' as follows:

  1. name() { echo "Javatpoint";}  

To export the above function, execute the command as follows:

  1. export -f name  

Now, invoke the bash shell to execute the function:

  1. bash  

To call the function, enter the function name:

  1. name  

Consider the below output:

Linux export Command

let's create another function 'hello,' execute the command as follows:

  1. function hello  
  2. > {  
  3. > echo hello, welcome to javatpoint  
  4. > }  

To export the above function, execute the command as follows:

  1. export -f hello  

Consider the below output:

Linux export Command

Example4: Assign a value before exporting a function or variable:

The export command allows us to assign a value before exporting a function. Consider the below command:

  1. export name[=value]  

For example, assign a value to a variable as follows:

  1. a=5  

now export it as:

  1. export a  

we can verify the assignment by using the printenv command as follows:

  1. printenv a  

Consider the below output:

Linux export Command

Example5: Set vim as default editor:

The vim editor is the most widely used text editor for the Linux systems. We can set the vim as default text editor by using the export command.

To set the vim as a default text editor, execute the following command:

  1. export EDITOR=/usr/bin/vim  
  2. export | grep EDITOR  

The above commands will not show any confirmation. Consider the below output:

Linux export Command

Example6: Set an environment variable

To create a new variable, use the export command followed by a variable name and its value.

Syntax:

  1. export NAME=VALUE  

To create a new variable, 'sys,' execute the command as follows:

  1. export  sys=50  

The echo command is used to display the variable:

  1. echo sys  

To display the value of the variable, use the $ symbol before the variable name

  1. echo $sys  

Consider the below output:

Linux export Command

Linux Set Command

 Linux set command is used to set and unset certain flags or settings within the shell environment. These flags and settings determine the behavior of a defined script and help in executing the tasks without facing any issue. The values of shell attributes and parameters can be changed or displayed by using the set command.

Syntax:

  1. set [options]  

To know about how to set the environment variable in Linux, visit Here.

Options: The supported options by the set command are as following:

  • -a: It is used to mark variables that are modified or created for export.
  • -b: It is used to notify of job termination immediately.
  • -e: It is used to exit immediately if a command exits with a non-zero status.
  • -f: It is used to disable the file name generation (globbing).
  • -h: It is used to save the location of commands where they looked up.
  • -k: It is used to place all assignment arguments in the environment variable of a command, except those that precede the command name.
  • -m: It is used to enable Job control.
  • -n: It is used to read commands.
  • -o: It is used for option-name.
  • -p: It is used to disable the processing of the '$ENV' file and import shell functions. It is turned on whenever the real and effective user ids do not match. Turning off this option may cause the working uid and gid to be set as the authorized uid and gid.
  • -t: It is used to exit from the command after executing one command.
  • -u: It is used to treat unset variables as an error when substituting.
  • -v: It is used to print shell input lines.
  • -x: It is used to print commands and their arguments in a sequential way (as they are executed).
  • -B: It is used to perform brace expansion by the Shell.
  • -C: It is used to disallow existing regular files to be overwritten by redirection of output.
  • -E: It is used if the ERR trap is inherited by the shell functions.
  • -H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
  • -P: It is used if we do not want to follow symbolic links when executing commands.
  • -T: If this flag is set, the DEBUG trap is inherited by the shell functions.

Note: To turn off the above flags, use the '+' rather than '-' symbol.

To better understand the set command, let's see a brief introduction of the shell variable:

Shell Variable

A variable is a character string that can hold a value. The assigned value could be anything such as number, filename, text, or any other data type. It is just like a pointer to the actual data. The shell allows us to create, delete, and assign variables.

The variable name can have any letters, numbers, or underscore (_) character. We cannot have the characters such as "! *, or -", because these special characters have other meanings for the shell. According to Unix naming convention, the Unix shell variable must have their names in UPPERCASE.

Examples of the set command

Turn debugging information on or off.

The '-x' option is used with the set command to show command and their arguments. It is useful for debugging the shell script.

Execute the following command:

To turn on the debugging information:

  1. set -x  

To turn off the debugging information:

  1. set +x  

Disable Bash's default behavior.

To disable the default behavior of Bash, execute the command as follows:

  1. set -C  

Stop a script immediately.

To stop a script immediately, execute the following command:

  1. set -e  

Getting Help

If you get stuck during the use of the set command, you can access the help documentation from your terminal. To access the help manual, execute the below command:

  1. set --help