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  

Linux Set Environment Variable

 The environment variables are dynamic values that are stored within a system and used by applications launched in shells or sub-shells. These variables have a name and their respected value. The environment variable customizes the system performance and the behavior of an application.

The environment is the track for a computer application to interact with the system. The environment variable can have information about the default applications of the system, the system locale, the path of the executable file and the keyboard layout setting, and more. The environment variable makes an app available as per the system.

Common Environment Variables

Some standard environment variables are as follows:

  • PATH

This variable contains a list of directories in which our system looks for files. It separates directories by a (:) colon.

  • USER

This variable holds the username.

  • HOME

This variable holds the default path to the user's home directory.

  • EDITOR

This variable contains the path to the specified editor.

  • UID

This variable contains the path to the user's unique id.

  • TERM

This variable contains the path to the default terminal emulator.

  • SHELL

This variable contains the path to the default shell that is being used by the user.

  • ENV

This variable displays all the environment variable.

How to set Environment Variable in Linux?

There are multiple commands in Linux that allow us to set and create the environment variable.

Use the export command to set a new 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 say new_variable, execute the command as follows:

  1. export new_variable=10  

The echo command is used to display the variable:

  1. echo new_variable  

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

  1. echo $new_variable  

Consider the below output:

Linux Set Environment Variable

To set Java Environment Variable, execute the command as follows:

  1. export PATH=$PATH:/home/jdk1.8/bin/  

We can also create a user to define a variable by directly declaring it on the terminal.

If we want to create a variable new_variable2, we can create it as follows:

  1. new_variable2=100  

Consider the below output:

Linux Set Environment Variable

Accessing the value of Environment Variable

To access the value of a variable, execute the echo command as follows:

  1. echo $variable name  

Note: The variables are case sensitive; we cannot use any variable name 'new_variable' as 'New_variable.'

The env command

The env command is used to display all the available variables in the system.

  1. env   

Output:

SHELL=/bin/bash
SESSION_MANAGER=local/javatpoint-GB-BXBT-2807:@/tmp/.ICE-unix/1458,unix/javatpoint-GB-BXBT-2807:/tmp/.ICE-unix/1458
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GTK_IM_MODULE=ibus
LANGUAGE=en_IN:en
QT4_IM_MODULE=ibus
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
SSH_AGENT_PID=1362
GTK_MODULES=gail:atk-bridge
PWD=/home/javatpoint
LOGNAME=javatpoint
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
XAUTHORITY=/run/user/1000/gdm/Xauthority
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
WINDOWPATH=2
HOME=/home/javatpoint
USERNAME=javatpoint
IM_CONFIG_PHASE=1
LANG=en_IN
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
XDG_CURRENT_DESKTOP=ubuntu:GNOME
VTE_VERSION=5802
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/dc6e48ed_7868_43d5_a086_fd6d6a90a74a
INVOCATION_ID=fadf556d1cf5422ea55b247bf57c3c99
MANAGERPID=1252
CLUTTER_IM_MODULE=ibus
GJS_DEBUG_OUTPUT=stderr
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
LESSOPEN=| /usr/bin/lesspipe %s
USER=javatpoint
GNOME_TERMINAL_SERVICE=:1.216
DISPLAY=:0
SHLVL=1
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
JOURNAL_STREAM=9:35179
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
_=/usr/bin/env
javatpoint@javatpoint-GB-BXBT-2807:~$ 

Removing an Environment Variable

By removing an environment variable we can remove all existing component of particular variable.

To remove an environment variable, execute the unset command followed by variable name:

  1. unset variable name  

The above command will delete the specified variable and its components from the system.

To remove a variable new_variable from the system, execute the command as follows:

  1. unset new_variable  

Consider the below output:

Linux Set Environment Variable