bash snippet



Quickly find what disk is mounted for your current working directory
Hi,

i suggest to use
Code:
df -h .

this display the disk (partition) of the current working directory with space in human readable format .
 
Hi,

i suggest to use
Code:
df -h .

this display the disk (partition) of the current working directory with space in human readable format .

The irony is that I find that option "less-human-readable".
You see, for me

100000

looks more than

1000

This while

1000M

does not look more than

1000K

to me, without first converting all letters into types and values and doing calculations.
Certainly, if arbitrarily listed in 1 column with many, many entries.
Even worse if more than 2 letters are involved.

So, "df -k" it is, because it's a good base.
 
Some interesting options for use with the CP (copy) command

cp -p source.txt target.txt

By default, Linux wouldn't copy the timestamp of the source file to the target file. It sets the time of the act of copying, to the target file. Now, whether or not that is a good idea as a default, is up to debate, but just this : in Windows (NTFS) copying the source time to the target file, is the standard behavior. Note that this is about target files, not target directories. But in Linux, in case you want to preserve the time - for example to indicate the backup file I made is (exactly) the same as the source - I would use this option.

Typical for Linux as well is that many commands are quite silent, when it comes to output. So is CP. But you can change this behavior. It even displays source AND target file names, which is interesting.

cp -v source.txt target.txt

You can also use an option to just copy "new" files, which means files not existing as target, or when the target file is older than the source. This doesn't conflict with the usage of -p because when omitting -p the target files are newer, not older.

cp -u source.txt target.txt

And if you combine these interesting options, you can simply use:

cp -pvu source.txt target.txt
 
Haha, good one

Suppose you want to time a given command or whatever on the command line, you can just prefix the whole thing with "time". For example if your FIND command runs too long once more, start measuring it:

time find / -name 'notanameofafile' 2> /dev/null

Apart from getting the time needed to perform the command, you obviously also get the normal output of the command in question.
If you have issues with directories having lots of files (millions for example), you can measure how bad it is:

time ls -l /directory/with/many/files
 
The “help“ command is a bash built-in command, used to display information for bash built-in commands.
help (option) (perimeter)
-d: Display a brief description of the built in commands.
-m: Output the information of built in commands in the format of the man manual.
-o: Only output the command format of built-in commands.

Speaking of help,
the obvious thing to mention is MAN
(from: manual)

Any Bash command, prefix with MAN:


Will get you lots of info, here about the CP command.
There's usually also a minus help or question mark, or whatever, but the idea was that MAN should work for all commands, and it has more info than the help feedback of the command in question. Granted, the help page of the command is often short and clear, so if it is something simple, it may work better.
 
To find your downtime, look for "100% packet loss" in the file that is created on the desktop:
Code:
cat ~/Desktop/testnetwork.txt | grep 100%

Technically you can CAT and GREP like in the above, but GREP also accepts a file as input parameter.

So :

cat whatever.txt | grep something

is the same as this - shorter, and thus faster - command:

grep something whatever.txt
 
If you want to find a process with a given Process ID (PID) you can use PS and GREP
(.. but I'm not showing that command now).
Instead you can use the predefined parameter for that.
The variant using GREP is less good because it will also show any parent or child
process with such an SID, or when it is a "short" number, it may show processes
with that subset of numbers. Even worse it will show commands with that number
anywhere in the process full description, if matching.
So instead, use:

ps -fp 12345


It's quite simple to use loops on files in Bash,
example:

for file in *.txt ; do cat $file ; done

That's it.
I'm using semicolons ( ; ) here because the FOR command is a multi-line command
by design. You can work around this by creating a line feed in the form of the
semicolon character, which normally means some command ended here, and the new
one starts now. But in multi-line commands (like FOR) it behaves as a line feed
(enter).
 
If you want to find a process with a given Process ID (PID) you can use PS and GREP
(.. but I'm not showing that command now).
Instead you can use the predefined parameter for that.
The variant using GREP is less good because it will also show any parent or child
process with such an SID, or when it is a "short" number, it may show processes
with that subset of numbers. Even worse it will show commands with that number
anywhere in the process full description, if matching.
So instead, use:




It's quite simple to use loops on files in Bash,
example:



That's it.
I'm using semicolons ( ; ) here because the FOR command is a multi-line command
by design. You can work around this by creating a line feed in the form of the
semicolon character, which normally means some command ended here, and the new
one starts now. But in multi-line commands (like FOR) it behaves as a line feed
(enter).
Rather than using ps and grep together, you could just use pgrep, which is part of the procps package (which provides us with the ps command and various other tools for getting information from /proc).
So, in order to see only information for a parent process that has multiple child-processes open, without seeing the child processes info - I'd do something like this:
Bash:
pgrep nameofprocess | xargs ps -fp
or
Bash:
ps -fp "$(pgrep nameofprocess)"
In the above, we use pgrep to get the pid of the main process and then use ps to get information about it.

For example, if I'm running firefox and I want to see some information about the main process, without seeing the child-processes:
Bash:
ps -fp "$(pgrep firefox)"
And that will show me the following information:
Code:
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75543    8403  7 21:28 ?        00:04:25 firefox-esr

And if I want to view information about the main process and all of it's children, I could use pgrep -f firefox command to get the PIDs of the main process and it's children and redirect them into while read loop to use the ps -fp command on each pid.
like this:
Bash:
while read pid; do ps -fp "$pid"; done < <(pgrep -f firefox)

Which will yield information like this:
Code:
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75543    8403  8 21:28 ?        00:05:51 firefox-esr
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75609   75543  0 21:29 ?        00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20231113155436 -prefsLen 40436 -prefMapSize 247635 -appDir /usr/lib/firefox-esr/b
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75676   75543  1 21:29 ?        00:00:46 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 2 -isForBrowser -prefsLen 46063 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75715   75543  9 21:29 ?        00:06:23 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 3 -isForBrowser -prefsLen 34336 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID          PID    PPID  C STIME TTY          TIME CMD
jason      75882   75543  0 21:29 ?        00:00:05 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 7 -isForBrowser -prefsLen 34393 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID          PID    PPID  C STIME TTY          TIME CMD
jason      78563   75543  0 22:06 ?        00:00:02 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 18 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      78696   75543  0 22:09 ?        00:00:09 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 19 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      79139   75543  2 22:15 ?        00:00:31 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 20 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      80365   75543  0 22:27 ?        00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 25 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      80542   75543  0 22:30 ?        00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 26 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      80608   75543  0 22:31 ?        00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 27 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID          PID    PPID  C STIME TTY          TIME CMD
jason      80730   75543  0 22:33 ?        00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 28 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2

The first PID is the main firefox-esr process.
All of the others are it's children.
 
Last edited:
Oh yes, PGREP has a bigger set of filters but the objective of PGREP is to search on process name, or for parent/child combos.

While if the PID is already known, the PS command can just filter on PID perfectly already. You can't beat the simplicity of that command with PGREP.

For process name search I agree to use PGREP instead of PS (which is done a lot as well in many scripts ... I have one script, not writtren by me obviously that each time kills processes it doesn't need to because the filter is setup incorrectly and it's using PS).
 
Last edited:
I use this one to color my prompt so that it is easy to see where a command/script starts or ends

export PS1='$(tput setaf 2)[\u@\h:\w] $ $(tput sgr0)'
 
A bit longer snippet, but I use this for notes via SFTP to a VPS

Code:
notes() {
    VAR=$1
    if [ -z $VAR ]; then
        /usr/local/bin/vim sftp://[email protected]/notes/default.txt
    elif [ "$VAR" == "-h" ]; then
        printf "Usage: notes [-l] [file]\n";
        printf "  notes             Open/Create \"default.txt\" file\n"
        printf "  notes  <file>     Open/Create \"<file>.txt\" file\n"
        printf "  notes  -l         Show available note files\n"
        printf "  notes  -h         Show this help\n"
    elif [ "$VAR" == "-l" ]; then
        printf "List of notes:\n"
        ssh [email protected] "ls -1 \$HOME/notes/*.txt | sed 's,.*/\(.*\)\.txt,  - \1,g'"
    else
        vim sftp://[email protected]/notes/$1.txt
    fi
}
 

Members online


Top