Linux Shell Foo
Find
Get the most recently modified file from a directory:
|
|
Find the 10 largest files of a directory and its sub directories recursively:
|
|
Find all files between 10Mb and 20Mb
|
|
Find all files created after 00:00:00 31.12.2020
|
|
Find all files created before 00:00:00 31.12.2020
|
|
Find and delete all files with the name special.txt
and delete them
|
|
Locate
The locate command does not need to walk the whole file system to locate a file you search for. It uses a database which can be updated by the following command:
|
|
After that you can use search strings with wildcards to locate specific files like this:
|
|
One drawback, in comparison to find, is that it does not provide as many search filters.
column
To view multi line output formatted as table you can use the column
command with the -t
flag.
For example, we want to view the inode number and the name of every file in a directory. Without column the output looks like this:
|
|
we can pipe it into column -t
to format it:
|
|
awk
Using netstat
list all services listening on all interfaces (0.0.0.0) for IPv4:
|
|
grep
regex
To use the patterns run grep
with the -E
flag for extended regex
Operator | Description |
---|---|
(a) | The round brackets are used to group parts of a regex. Within the brackets, you can define further patterns which should be processed together. |
[a-z] | The square brackets are used to define character classes. Inside the brackets, you can specify a list of characters to search for. |
{1,10} | The curly brackets are used to define quantifiers. Inside the brackets, you can specify a number or a range that indicates how often a previous pattern should be repeated. |
| | Also called the OR operator and shows results when one of the two expressions matches |
.* | Also called the AND operator and displayed results only if both expressions match |
systemctl
Nice looking list of all services
|
|
Add Service to SysV to start automatically
|
|
Background a process
It is possible to suspend process execution and send it to the background by pressing Ctr-z
to send a SIGSTP
signal to the kernel which suspends the process.
We can resume execution by running the fg
command which puts the process back to the foreground.
This can come in handy if you are in your text editor and just want to quickly switch out of it, run a command and then switch back to it.
You can also start a process which will run in the background and send its result to your shell as soon as it exits by adding a &
to the end:
|
|
rsync
Transfer files from a directory, keeping file attributes like permissions, using compression for quicker transmission and ssh for encrypted transport:
|
|
mount
List all mounted drives:
|
|
Mount a Usb stick to the local filesystem to the directory /mnt/usb
:
|
|
Unmount the Usb stick:
|
|
ifconfig
Assign net mask to a network interface
|
|
Assign an IP address to a network interface
|
|
route
Set the address default gateway for a network interface to be used to send traffic outside the current network the interface is connected to
|
|
tee
If you want to redirect the output of a command in a sequence of commands connected by pipes you can use tee
to redirect output to a file in between chained commands:
|
|
If you want to run tee
as part of a loop and append to a file use the -a
flag
|
|
Keyboard Shortcuts
CTRL-a
move the cursor to the beginning of the current lineCTRL-e
move the cursor to the end of the current lineCTRL-<left-arrow|right-arrow>
jump to the beginning of the current/previous wordALT-B
jump back one wordALT-F
jump one word forwardCTRL-R
search through the command historyCTRL-u
erase everything from the current position of the cursor to the beginning if the lineCTRL-k
erase everything from the current position of the cursor to the end of the lineCTRL-w
erase the word preceding the cursor position