Amazing Linux Command Line Tips and Tricks

Linux Command Line Tips & Tricks

In an operating system, Command line is a tool which provides the typing interface to perform commands on computer. This article will walk you trough some time saving tips to working with Linux command line.

Switch working directory to home directory

cd ~

In Linux ~ refers to home directory. You can use cd ~ to switch current working directory to home directory.

Execute previous command

!!

Print Or get process ID of current Shell

echo $$

Inside script “$$” return the process ID of the process in which it is executing

List last n commands used

Command history returns history of commands performed typically 1000 or configured count

history 

Pass number to get last n commands

history 5

Repeat last nth command

!-n executes the nth last command from the history. Where following command executes last 5th command

!-5

Repeat nth command from history

!n executes the command by it’s number from history . Where following command executes the command at 535th entry in command history.

!535

Repeat last command starting with given characters

!string executes the very last command which starts with given string of characters. For example following command executes last command which starts with lsb .

!lsb
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 140.7M 1 loop /snap/gnome-3-26-1604/74
loop1 7:1 0 143.5M 1 loop /snap/gnome-3-28-1804/23

Executes last command which contains given characters

!?chars executes the last command which contains given characters. For Example following example executes command which contains getKT.

echo "hello! welcome to getKT"
hello! welcome to getKT
!?getKT
echo "hello! welcome to getKT"
hello! welcome to getKT

Reuse the last word from previous command

!$ refers to the last word in the last command executed in the terminal. In the following example we used created file name using !$ .

echo "hello world" > testfile
# cat !$
cat testfile
hello world

Get process number of last background command

echo $!

Populate command from history while typing

CTRL + R populates last command that matches as you type

~#
(reverse-i-search)`if': ifconfig

Execute command in background

To execute given command in background append & to the command

# command &

For example, the command “sleep 50 &” would execute the command in background because of suffix &. So, it doesn’t block command line.

sleep 50 &
[2] 9416

Suspend the current command

CTRL + z suspends long running command and puts it in background to gives you access to the terminal .

Send command to background

Command which is sent to background can be called as background job (task) . The command bg is used to put given command by it’s JOB_SPEC to background.

# bg --help
bg: bg [job_spec ...]
    Move jobs to the background.
    
    Place the jobs identified by each JOB_SPEC in the background, as if they
    had been started with `&'.  If JOB_SPEC is not present, the shell's notion
    of the current job is used.
    
    Exit Status:
    Returns success unless job control is not enabled or an error occurs.

Where job_spec can be,

  • %n : Job number n
  • %str : Refers to job command which start with str
  • %?str : Refers to job command which contains str
  • %% or %+ : Refers current job
  • %- : Refers the previous job

Bring last background job back to foreground

command fg brings the last background job to foreground. The command which executes in foreground can be called as foreground job(task)

fg

Bring last nth background job to foregrond

fg n brings last nth background job to foreground where n is background job ID not PID

fg 3

jobs command lists all background jobs by it’s job ID and command

Easy cursor navigation by words and lines

Following short cuts will help you to navigate cursor quickly in side command line

Move cursor left by one wordESC + B
Move cursor right by one wordESC + F
Jump to the beginning of the line CTRL + A
Jump to the end of the lineCTRL + E
Delete the previous word CTRL + W
Clear the lineCTRL + U

Unfreeze Linux terminal from accidental CTRL + S

If you freeze your terminal because of habitual CTRL+S. Don’t worry frozen terminal can be unfrozen by CTRL + Q .

Linux terminal copy and paste

Most of the terminals support copy and paste with following short cuts

CopyCTRL + SHIFT+ C
PasteCTRL + SHIFT + V

Also you can use middle mouse button to paste copied or selected text.

Some terminal may support CTRL +C and CTRL +V. On windows on putty, select text for copy and then right click to paste

Linux Shell Command Tips For Files and Directories

Linux Shell Command Tips For Files and Directories

This article would give you quick Linux command tips to perform operation and work with files and directories effectively

Create Multiple Directories at once

mkdir  path/to/{test,work,pictures,newDir}

No spaces around comma ( , ) in side curly braces {}

Above command creates all given directories inside curly braces {} at specified location. Here in this case “test, work, pictures and newDir”. This command will create path/to/test, path/to/work, path/to/pictures and path/to/newDir

Create Multiple Files Using touch Command At once

Multiple files can be created at once by passing names enclosed by curly braces {} and separated by comma (,)

touch {pictures,work,fun} 
touch /path/to/{pictures,work,fun} 

Remove All Files Except Specified

This is my favorite command. Using following syntax you can remove all files except the one or more specified using command rm .

rm !(<file_to_keep>|<another_file_to_keep>)

Let’s say we have following files

ls
cg  manage  python  test  world

We want to keep files test and cg and delete rest, here is the way

rm !(test|cg)

Same thing can also be achieved by using the command find .

find . -type f ! -name 'filename'  -delete

Create Directories Recursively

Use command line option -p on command “mkdir” to create directories recursively without getting error

mkdir -p /home/demo/server/www

Since we passed option -p, mkdir command will create all directories recursively if any directory in the specified path doesn’t exist

Rename Files By Using Regular Expression

There is a handy command called “rename” to rename multiple files according to given pattern. This rename command uses the sed command syntax.

Let’s learn this command by example. For suppose we have following files,

ls
testfile  work  worker  workspace

I want to rename/replace work in every file to test. We can achieve this by following command

rename 's/work/test/' * 

As said syntax is similar to command sed. Describing ‘s/work/test’. Where,

  • s is the command which stands for substitute
  • work in ‘s/work/test’ is the text to replace
  • test in ‘s/work/test’ is the text to get replaced

If you want to see what files are going to get renamed instead of taking action. You can do it so by passing command line argument -n .

rename -n 's/work/test/' *
rename(work, test)
rename(worker, tester)
rename(workspace, testspace)

Display File Type Based On Content

We humans and some programs recognize the type of file by it’s extension but it is not reliable. Sometimes actual file type could be something else than file extension it has. Actual file type by it’s content can be displayed using command “file” .

$ file <path/to/file>

Command file is more capable than querying file type

Pass desired file path (absolute or relative) to the command file to get it’s type

file welcome.mp3
welcome.mp3: MPEG ADTS, layer III, v2,  24 kbps, 16 kHz, Monaural

Query mime type of file instead of verbose description with command line option -i .

file -i  welcome.mp3
welcome.mp3: audio/mpeg; charset=binary

Print only File type without echoing file name

file -b cg
ASCII text

Create Symbolic Links

Basically there are two types of symbolic links, those are Hard Link and Soft (Symbolic) Link . Think of them as short cuts on windows

Hard links associate file names of created links with same inode. As a result they share the same data block on the disk. It is perceived as independent copy of files.

A file is said to be completely delete only if the file and all of it’s hard links are deleted.

Symbolic Link or Symlink or Soft Link is a file which points to another file. Unlike hard Links, soft links point to path on the file system instead of physical location(inode).

Hard linking is not allowed for directories, hard links can only be created for files. Where as, soft links can be created for both files and directories

Command ln is used to create Symbolic Links

ln <source> <link>

Create Hard Link

ln  /path/to/source  /path/to/link

Create Soft Link

The command ln created soft link command line argument -s is passed

ln -s  /path/to/source  /path/to/link

Display File Meta Data

The command stat can be used to display file or file system meta data.

stat testfile
File: testfile
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 1612373 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-03-25 21:30:20.240532016 +0530
Modify: 2019-03-25 21:30:20.240532016 +0530
Change: 2019-03-25 21:30:20.240532016 +0530
Birth: -

Move All Files and Folders Except Specified

Following command would help you to move all files and folder in current working directory except specified once.

mv !(<file_to_keep>|<another_file_to_keep>) destination

ls
 abc  fileA  fileB  mdir  test  xyz

Following command will move all but specified to destination directory

mv !(fileA|fileB)   /opt/dst

Above command will move all files but specified fileA and fileB to destination /opt/dst

Move all files but specified using the command find

We can also move all but specified using the command find. Using the command find will give us advantage of more fine control over filters.

Move file & folders inside current working directory to specified destination

find -maxdepth 1 -not -name "abc" -exec mv {} /opt/dst \;

Describing above command. Where,

  • Option -maxdepth 1 will force find to look for files & folders in current working or given directory only
  • Option -not negates the match. Spits out all but matched
  • Option –name “abc” . -name takes string too look up files which contain given name. Here, it means to look for file named or contains “abc”.
  • -exec takes external command to execute on matched files.
  • {} will be replaced by matched file/folder path in every match.

You can spice it up, if you are comfortable with regular expressions to match multiple files

find -maxdepth 1 -type f  -regextype egrep -not  -regex './(fileA|fileB)' -exec mv {} /opt/dst \; 

Above command match only files (-type f) other than fileA and fileB and move
(-exec mv {} /opt/dst \;) operation on matched

Switch to Previous Working Directory

As we switch directories or change current working directory using command cd . Linux updates the environment variable both OLDPWD and PWD. we can use environment variable OLDPWD to switch back to previous working directory.

Switch to old working directory

cd $OLDPWD

We can also switch to previous working directory using “cd -“

cd -

List Hidden Files

List all files including hidden files

ls -a
ls -la

Delete Files Based On Their Age

The command find can be used to filter files or folder by their age and perform desired action

find /path/to/files -type f -mtime <time> -exec rm {} \;

find /path/to/files -type f  -mtime +7 -exec rm {} \;

Describing Above Command

  • /path/to/files is the path to look for files
  • -type f is to limit find lookup by files
  • -mtime +5 => -mtime is used to specify time like how old the file can be. +7 means, find files older than 7 days.
  • -exec allows you to execute external command on filters such as rm.
  • {} will be replaced by found file/folder.
  • ;\ is required at the end for this command to work.

Display Files Inside tar file or archive

To just check what is inside an archive, we don’t need to extract it. Following commands can be used to see files inside archive.

tar -tf   filename.tar

Read PDF File In Linux Terminal

Well, there is nothing magical we need to read PDF files on Linux command line (terminal).All you need is the command less .

less /path/to/file.pdf

Empty The File Without Deleting

If you want remove the content of a file instead of deleting it, you don’t have to open a file. Instead you can use the following syntax to empty it.

>  filename

Print First n Lines Of A File

The command head helps you print first n lines of file. It takes command line option -n by default it is 10 if not passed

head filename

Following command prints first 5 lines since command line arg -n 5 is passed

head -n 5 filename

Print Last n Lines Of A File

The command tail helps you to print last n lines of the file. Number of lines it prints is 10 by default if n is not passed.

tail filename

Following command prints only last 5 lines since command line arg -n 5 is passed

tail -n 5 filename

If file tend to change often (ex: log file) we can force tail command to listen for added lines at the end and print on console

tail -f filename.log

Types of function arguments in python

Python Function - Argument Types

Unlike many other programming languages python supports multiple types formal arguments in the function definition. It is one of the flexibility that python offers.

Types of function (formal) arguments that python supports,

Following picture illustrates these different types of arguments by their order according to the syntax and how we can use them.

Python Function Definition, Arguments Kind and Order
Python Function Definition, Arguments Kind and Order

Let’s see these arguments in detail. Before we proceed you need to know difference between formal arguments and actual arguments.

Formal Arguments VS Actual Arguments

Formal arguments are identifiers used in the function definition to represent corresponding actual arguments.
Actual arguments are values(or variables)/expressions that are used inside the parentheses of a function call.

Above definition and terminology of Formal/Actual arguments is same across different programming languages.

Regular Positional Arguments

These are regular positional argument as it sounds regular. Regular positional arguments are very much similar to arguments that you see in any kind of programming language. These are just (variables) identifiers that values of actual arguments are copied to formal arguments by their position in the function call *.

def add(a, b):
return a + b
# function call
add(2, 3)

If count of actual arguments and formal arguments don’t match interpreter will throw TypeError exception

>>> add(2)
Traceback (most recent call last):
File "", line 1, in <module>
TypeError: add() missing 1 required positional argument: 'b'

Default Arguments

Default arguments allow us to provide defaults for corresponding formal arguments in the function definition. Python uses these defaults if corresponding actual arguments are passed in the function call.

Default arguments should always be followed by non-default arguments in function definition

def greetings(name, msg="Welcom, Good Morning"):
message = "Hello {}! {}".format(name, msg)
print(message)

As we can see here, the argument msg got it’s default in the function definition. If the argument msg is not passed in the function call it’s default given in the definition will be used. So called default arguments.

>>> greetings("neo")
Hello neo! Welcom, Good Morning

Keyword Arguments

Keyword arguments are passed by it’s name instead of their position as opposed to positional arguments in the function call. As a result we don’t have to mind about position of arguments when calling a function.

Keyword arguments should always be followed by positional arguments in the function call

Let’s call above defined function greetings using keyword arguments

greetings(name="neo", msg="Welcome")

Since we are passing arguments by their names/keywords order or position doesn’t matter

greetings(msg="Welcome", name="neo")

We can mix both positional and keyword arguments but keyword arguments should follow positional arguments

greetings("neo", msg="Welcome")

If you break the rule and pass arguments in such a way that positional arguments follow keyword arguments which is a syntax error.

>>> greetings(msg="Welcome", "neo")
... ...
SyntaxError: positional argument follows keyword argument

Variable Length Positional Arguments

Variable length positional arguments are useful when we are not certain about number of arguments that are required in the function definition or function would receive. It is quite useful to write functions that take countless number of positional arguments for certain use cases.

Syntax to receive variable length positional arguments

def foo(a, b, c, *varargs):
....

Python first fills the slots given formal arguments by passed positional actual arguments. Any left over actual (positional) arguments are captured by python as a tuple, which is available through variable which is having prefixed * in the function definition.

As we can see in the above syntax. variables a, b, c need to be filled by actual arguments any left over passed arguments can be accessed as tuple using varargs.

def foo(a, b, c, *varargs):
print("Regular Positional Arguments", a, b, c)
print("Variable Length Positional arguments", varargs)
>>> foo(2, 3, 4)
Regular Positional Arguments 2 3 4
Variable Length Positional arguments ()

Since there are no extra positional arguments are passed in the above function call. The variable varargs is a empty tuple

>>> foo(1, 2, 3, 4, 5, 7)
Regular Positional Arguments 1 2 3
Variable Length Positional arguments (4, 5, 7)

As you can see in the above function call, left over arguments are added to tuple and can be accessed using given variable varargs.

You can unpack and pass each element in the sequence as individual argument just by prefixing * in front of the sequence

>>> foo(1, 2, 3, *'hello')
Regular Positional Arguments 1 2 3
Variable Length Positional arguments ('h', 'e', 'l', 'l', 'o')

Where as, if you don’t put * in front of a string or sequence in the function call. Which will be taken as single argument as a whole.

>>> foo(1, 2, 3, 'hello')
Regular Positional Arguments 1 2 3
Variable Length Positional arguments ('hello',)

keyword Only Arguments

Keyword Only arguments are new feature in python 3. Main purpose of keyword only arguments is to be able to pass arguments by it’s keyword only for selected arguments.

Read More here at python keyword only arguments

There are two types of keyword only arguments

Variable Length Keyword Arguments

Variable length keyword arguments are very much similar to variable length positional arguments. It is the mechanism to accept variable length arguments in the form keyword arguments which is useful if we are not certain about number of keyword arguments that function receives. Python captures all extra keyword arguments as dictionary and makes it available by name(variable) which is prefixed by ** in the function definition.

Variable Length Keyword Arguments Syntax

def foo(a, b, c, **kwargs):
. . .

kwargs gets left over keyword arguments after formal arguments are filled up by passed keyword arguments.

def printKWArgs(a, b, c, **kwargs):
print("Regular Positional Arguments", a, b, c)
print("Variable Length keyword arguments", kwargs)
>>> printKWArgs(2, 3, c=5)
Regular Positional Arguments 2 3 5
Variable Length keyword arguments {}

In the above function call since we haven’t passed any extra keyword arguments variable kwargs is a empty dictionary.

>>> printKWArgs(2, 3, c=4, d=9, e=10)
Regular Positional Arguments 2 3 4
Variable Length keyword arguments {'d': 9, 'e': 10}

In the above function call left over keyword arguments d and e can be accessed through variable kwargs which is dictionary. Where key will be the passed keyword name and value will be value given for corresponding keyword in the function call.

Combining both varargs and kwargs

You can use both variable length positional arguments and variable length keyword arguments combined.

def foo(a, b, *varargs, **kwargs):
...

Different Types Of Function Arguments In Action

Here is the simple function which makes use of all kind of arguments and prints passed arguments by it’s kind

Let’s call the above function by passing these several kind of arguments

printArgsByKind(1, 2, 3, 3.15, float, 4, 5, 6, operation="count", return_type=int, abc=3, xyz=9)

The above function call would print the given arguments by it’s kind as follows,

Regular Positional Arguments a: 1, b: 2, c: 3
Default Arguments default: 3.15, intype: <class 'float'>
Variable length positional arguments(tuple) more: (4, 5, 6)
Non-Default Keyword-Only Arguments operation: count, return_type: <class 'int'>
Default Keyword-Only Arguments print_result: False, ignore_exception: True
Variable length keyword arguments kwargs: {'abc': 3, 'xyz': 9}