Post

Linux

The shell

Wildcards

Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters.

Alt text

Commonly Used Character Classes

Alt text

Using wildcards makes it possible to construct sophisticated selection criteria for filenames.

redirecting files

I/O redirection allows us to redefine where standard output goes. To redirect standard output to another file instead of the screen, we use the > redirection operator followed by the name of the file.

1
2
3
4
└─# ls -l /usr/bin > ls-output.txt

└─# ls -l ls-output.txt           
-rw-r--r-- 1 root root 246845 Apr  2 10:32 ls-output.txt

Redirecting Standard Error

What if the file does not exit and we don’t want the error to be displayed on the screen l.e

1
2
3
4
5
6
7
8
9
10
└─# ls -l /bin/usr > ls-output.txt
ls: cannot access '/bin/usr': No such file or directory

└─# cat ls-output.txt 

└─# ls -l /bin/usr 2> ls-error.txt

└─# cat ls-error.txt 
ls: cannot access '/bin/usr': No such file or directory
                                                                                          

Redirecting Standard Output and Standard Error to One File

1
2
3
4
5
└─# ls -l /bin/usr > ls-output.txt 2>&1

└─# cat ls-output.txt 
ls: cannot access '/bin/usr': No such file or directory
                                                            

Disposing of Unwanted Output

Sometimes “silence is golden” and we don’t want output from a command. This applies particularly to error and status messages. The system provides a way to do this by redirecting output to a special file called /dev/null.

ls -l /bin/usr 2> /dev/null

wc: Print Line, Word, and Byte Counts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
└─# wc -l sire | uniq  
13 sire

└─# cat sire                             
XJ0YL-75IJD-222N3-45FE2
sire
sire
sire
sire
1
2
3
4
4
56
7
                                               

└─# cat sire | uniq                      
XJ0YL-75IJD-222N3-45FE2
sire
1
2
3
4
56
7

└─# cat sire | uniq | wc -l
9

Brace Expansion

With Brace Expansion, you can create multiple text strings from a pattern containing braces.

1
2
└─# echo Front-{A,B,C}-Back 
Front-A-Back Front-B-Back Front-C-Back

Patterns to be brace expanded may contain a leading portion called a preamble and a trailing portion called a postscript. The brace expression itself may contain either a comma-separated list of strings or a range of integers or single characters. The pattern may not contain unquoted whitespace.

1
2
3
4
5
└─# echo Number_{1..5}
Number_1 Number_2 Number_3 Number_4 Number_5

└─# echo {01..15}            
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

Here is a range of letters in reverse order:

1
2
└─# echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Brace expansions may be nested.

1
2
└─# echo a{A{1,2},B{3,4}}b
aA1b aA2b aB3b aB4b

This method efficiently generates lists of files or directories, commonly used for organizing large collections such as images by “Year-Month” format, reducing manual input and potential errors.

1
2
3
4
5
6
7
8
9
10
11
└─# mkdir Photos
                                               
└─# cd Photos    
                                               
└─# mkdir {2007..2009}-{01..12}
                                           
└─# ls                                      
2007-01  2007-04  2007-07  2007-10  2008-01  2008-04  2008-07  2008-10  2009-01  2009-04  2009-07  2009-10
2007-02  2007-05  2007-08  2007-11  2008-02  2008-05  2008-08  2008-11  2009-02  2009-05  2009-08  2009-11
2007-03  2007-06  2007-09  2007-12  2008-03  2008-06  2008-09  2008-12  2009-03  2009-06  2009-09  2009-12

Command Substitution

Command substitution allows us to use the output of a command as an expansion.

1
2
└─# echo $(ls) 
dir1 dir2 fun fun-hard fun-sym ls-error.txt ls-output.txt numbers Photos sire

or we can not limit it to just echo command, we can alse use the command find Alt text

Adding the -e option to echo will enable interpretation of escape sequences. You can also place them inside $’ ‘

sleep 10; echo -e "Time's up\a"

AdvAnced KeyboArd TricKs

Cursor Movement

Alt text

Modifying Text

Alt text

Cutting and Pasting (Killing and Yanking) Text

Alt text

Permission Attribute

Alt text

File Modes in Binary and Octal

Alt text

chmod Symbolic Notation

Alt text

chmod Symbolic Notation Examples

Alt text

Processes

Networking

ping

The ping command sends a special network packet called an ICMP ECHO_REQUEST to a specified host. Most network devices receiving this packet will reply to it, allowing the network connection to be verified.

1
2
3
4
5
6
7
8
9
10
11
12
13
┌──(root㉿kali)-[/home/sire]
└─# ping -c 5 linuxcommand.org
PING linuxcommand.org (216.105.38.11) 56(84) bytes of data.
64 bytes from secureprojects.sourceforge.net (216.105.38.11): icmp_seq=1 ttl=42 time=304 ms
64 bytes from secureprojects.sourceforge.net (216.105.38.11): icmp_seq=2 ttl=42 time=311 ms
64 bytes from secureprojects.sourceforge.net (216.105.38.11): icmp_seq=3 ttl=42 time=302 ms
64 bytes from secureprojects.sourceforge.net (216.105.38.11): icmp_seq=4 ttl=42 time=315 ms
64 bytes from secureprojects.sourceforge.net (216.105.38.11): icmp_seq=5 ttl=42 time=305 ms

--- linuxcommand.org ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4011ms
rtt min/avg/max/mdev = 301.845/307.189/315.205/4.984 ms
                                                                       

traceroute

The traceroute program (some systems use the similar tracepath program instead) lists all the “hops” network traffic takes to get from the local system to a specified host.

Alt text

netstat

The netstat program is used to examine various network settings and statistics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
┌──(root㉿kali)-[/home/sire]
└─# netstat -r 
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.64.1    0.0.0.0         UG        0 0          0 eth0
10.0.3.0        0.0.0.0         255.255.255.0   U         0 0          0 lxcbr0
10.10.10.0      10.10.14.1      255.255.254.0   UG        0 0          0 tun0
10.10.14.0      0.0.0.0         255.255.254.0   U         0 0          0 tun0
10.129.0.0      10.10.14.1      255.255.0.0     UG        0 0          0 tun0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U         0 0          0 br-188301ac47c6
172.19.0.0      0.0.0.0         255.255.0.0     U         0 0          0 br-83d660d7963f
172.20.0.0      0.0.0.0         255.255.0.0     U         0 0          0 br-54f5e9f95c02
192.168.64.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0

Searching for fileS

locate—Find Files the Easy Way

a rapid database search of pathnames and then outputs every name that matches a given substring locate bin/zip

find—Find Files the Hard Way

Alt text

This post is licensed under CC BY 4.0 by the author.