Skip to content

Commit 0d1092c

Browse files
committed
Split many process and file utils.
1 parent 62bfb7a commit 0d1092c

27 files changed

+850
-763
lines changed

README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,44 @@ Generic data formats:
5656
- [unicode.md](unicode.md)
5757
- [xml/](xml/)
5858

59-
[virtual-machine.md/](virtual-machine): Vagrant, Docker.
59+
File and directory related utilities:
60+
61+
- [fdupes.md](fdupes.md)
62+
- [find.md](find.md)
63+
- [locate.md](locate.md)
64+
- [ls.md](ls.md)
65+
- [tree.md](tree.md)
6066

6167
Programming tools:
6268

6369
- [compile/](compile/): compilation process, dynamic libraries.
6470
- [ack.sh](ack.sh)
6571

72+
[virtual-machine.md/](virtual-machine/): Vagrant, Docker.
73+
74+
Process control:
75+
76+
- [chroot.sh](chroot.sh)
77+
- [env.sh](env.sh)
78+
- [jobs.sh](jobs.sh)
79+
- [lsof.md](lsof.md)
80+
- [nice.sh](nice.sh)
81+
- [nohup.sh](nohup.sh)
82+
- [ps.sh](ps.sh)
83+
- [pstree.sh](pstree.sh)
84+
- [pwd.sh](pwd.sh)
85+
- [sleep.sh](sleep.sh)
86+
- [top.sh](top.sh)
87+
- [wait.sh](wait.sh)
88+
89+
Other tools:
90+
91+
- [eval.sh](eval.sh)
92+
6693
Related subjects in other repositories:
6794

6895
- [networking](https://github.com/cirosantilli/net): networking tools and protocols: HTTP, SSH, curl Apache.
6996

70-
7197
#How to search for stuff
7298

7399
If you know what you are searching for, use the following methods to search by keyword.

chroot.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Execute single command with new root.
2+
3+
# The root of a process is a Linux concept: every process descriptor has a root field,
4+
# and system calls issued from that process only look from under the root (known as `/` to that process).
5+
6+
##application
7+
8+
# You have a partition that contains a linux system,
9+
# but for some reason you are unable to run it.
10+
11+
# You can use that partition with bash by using chroot into it,
12+
# and you might then try to fix it from there.
13+
14+
# Example:
15+
16+
sudo chroot /media/other_linux/
17+
18+
# More advanced example, if you want to start from a completelly clean bash environment:
19+
20+
sudo chroot /media/other_linux /bin/env -i \
21+
HOME=/root \
22+
TERM="$TERM" \
23+
PS1='\u:\w\$ ' \
24+
PATH=/bin:/usr/bin:/sbin:/usr/sbin \
25+
/bin/bash --login
26+
27+
# This will in addition clear enviroment variables, and read login scripts found on the chroot.
28+

env.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# POSIX 7
2+
3+
# Shows all environment variables and their values:
4+
5+
env
6+
7+
# Change environment for a single command:
8+
9+
a=b
10+
env a=c echo $a
11+
#c
12+
echo $a
13+
#b
14+
15+
# In bash it is also possible to do (not sure about portability):
16+
17+
a=b
18+
a=c echo $a
19+
#c
20+
echo $a
21+
#b
22+
23+
##-i
24+
25+
#exec in a clean environment:
26+
27+
[ "`env -i a=b env`" = "a=b" ] || exit 1
28+
29+
##start a subshell in the cleanest env possible
30+
31+
#don't forget: subshells inherit all exported vars
32+
33+
env -i bash --noprofile --norc
34+
env
35+
#some default vars might still be there!
36+
#I get: SHLVL, PWD
37+
exit

eval.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# POSIX 7.
2+
3+
# Exec string in current bash
4+
5+
eval "a=b"
6+
[ $a = b ] || exit 1
7+
8+
# Concatenates arguments, space separated:
9+
10+
[ `eval echo a` = a ] || exit 1
11+
12+
##applications
13+
14+
# Make varname from var>
15+
16+
a=b
17+
eval "$a=c"
18+
[ $b = c ] || exit 1
19+

fdupes.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#fdupes
2+
3+
Fine command line tool for eliminating byte by byte duplicates you can either:
4+
5+
- pick one by one
6+
7+
- tell fdupes to pick the first one without asking
8+
(seem to pick one of the directories first always)
9+
10+
Finds and prints dupes:
11+
12+
fdupes -r .
13+
14+
Finds dupes, and prompt which to keep for each match
15+
16+
fdupes -rd .

file.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#file
2+
3+
POSIX 7
4+
5+
Attempts to determine file type and retrieve metadata.
6+
7+
This is in general impossible,
8+
but program makes good guesses.
9+
10+
echo a > a
11+
file a
12+
13+
Output:
14+
15+
a: ASCII text
16+
17+
##L
18+
19+
Follow links:
20+
21+
$ echo a > a
22+
$ ln -s a b
23+
$ file b
24+
b: symbolic link to `a'
25+
$ file -L b
26+
b: ASCII text

filesystem.md

-26
Original file line numberDiff line numberDiff line change
@@ -782,32 +782,6 @@ List mounted filesystems:
782782

783783
cat /proc/mounts
784784

785-
#fuser
786-
787-
View which processes are using a device:
788-
789-
fuser -m /dev/sdb1
790-
791-
Useful if you want to unmount a filesystem, and you have to find out who is still using it.
792-
793-
#lsof
794-
795-
List all open files and pipes.
796-
797-
- `COMMAND`: process name.
798-
- `PID`: process ID
799-
- `USER`: username
800-
- `FD`: file descriptor
801-
- `TYPE`: node type of the file
802-
- `DEVICE`: device number
803-
- `SIZE`: file size
804-
- `NODE`: node number
805-
- `NAME`: full path of the file name.
806-
807-
Usage:
808-
809-
lsof
810-
811785
#fsck
812786

813787
File System Check.

fuser.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#fuser
2+
3+
View which processes are using a device.
4+
5+
On Ubuntu, comes from the `psmisc` package.
6+
7+
Similar to `lsof`.
8+
9+
exec 3<> /tmp/foo
10+
fuser /tmp/foo
11+
#/tmp/foo: 22924
12+
exec 3>&-
13+
fuser /tmp/foo
14+
#
15+
16+
Useful if you want to unmount a filesystem, and you have to find out who is still using it.
17+
18+
#k
19+
20+
Send `SIGKILL` to found process
21+
22+
#t
23+
24+
Search in given domain instead of file paths.
25+
26+
Possible values:
27+
28+
- `tcp`: TCP ports
29+
30+
Good combo with `k` to kill that pesky test server:
31+
32+
fuser -kn tcp 3000

jobs.sh

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Shows:
2+
3+
# - jobspec : a local job id.
4+
# - status : runnning, stopped, done
5+
# - invocation : exact program call, including command line args. Ex: `ls ~`
6+
7+
jobs
8+
9+
# Show pids of background jobs:
10+
11+
jobs -p
12+
13+
##jobspecs
14+
15+
# Local job id, found by using <#jobs>
16+
17+
# Certain commands such as `kill`, `fg` them in addition to pids.
18+
19+
# They are:
20+
21+
# - %N Job number [N]
22+
# - %S Invocation (command line) of job begins with string S
23+
# If several matches, ambiguous, and does nothing.
24+
# - ?S Invocation (command line) of job contains within it string S
25+
# - %% "current" job (last job stopped in foreground or started in background)
26+
# - %+ "current" job (last job stopped in foreground or started in background)
27+
# - %- last job
28+
29+
# It is possible to use jobspecs directly with certain bash built-ins that could also take PID.
30+
# For example, to kill process by jobspec `%1`:
31+
32+
#kill %1
33+
34+
# Note that `kill` also usually exists as an external executable, and that the external executable
35+
# cannot kill by jobspec since this information is only known by bash itself.
36+
37+
# `help kill` states that one of the reasons why `kill` is implemented as a bash built-in is to be
38+
# able to write `kill %1`.
39+
40+
#ls &
41+
#sleep 100 &
42+
#sleep 100 &
43+
#sleep 100 &
44+
#runs on background
45+
#
46+
#[1] 12345678
47+
#means local id 1
48+
#process number 12345678
49+
#
50+
#when process ends, it prints ``[n] 1234`` and disappears
51+
#
52+
#stdout continues to go to cur terminal, even if in bg

kernel/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,6 @@ and is now just waiting for the system to come and free its resources.
875875

876876
The following transitions are possible:
877877

878-
879878
+----------+ +--------+
880879
| | | |
881880
v | v |

kill.sh

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# POSIX 7
2+
3+
# On Ubuntu 12.04, implemented by the procps package.
4+
5+
# Kill exists as a bash built-in.
6+
# One of the reasons for this is to allow users to `kill` by jobspec for example as `sleep 1- &; kill %1`,
7+
# Which an external executable could not do. Killin gby PID is required by POSIX 7.
8+
9+
# Send signals to a process. Signals are an ANSI C concept, with POSIX and Linux extensions.
10+
11+
# Does not necessarily send SIGKILL, nor is SIGKILL the default signal sent!
12+
# The default signal it sends is SIGTERM.
13+
14+
# It is unfortunatelly named kill because most signals end up killing process,
15+
# or also because the most used signal is SIGTERM generated by a C-C on the terminal.
16+
# which has the usual effect of killing a process.
17+
18+
# List all signals available on the system:
19+
20+
kill -l
21+
22+
# Lists numbers and descriptions.
23+
24+
# Send SIGTERM signal to process:
25+
26+
ps -A
27+
ID=
28+
kill $ID
29+
30+
# SIGTERM is the default signal sent by `kill`.
31+
32+
# Select by pid, found on ps for example.
33+
34+
# Select by job-id found on jobs:
35+
36+
sleep 10 &
37+
jobs
38+
kill %1
39+
40+
# POSIX specifies this.
41+
42+
# Send stop signal to process:
43+
44+
kill -s SIGSTOP $ID
45+
kill -s sigstop $ID
46+
kill -s STOP $ID
47+
kill -s stop $ID
48+
49+
# All of the above are specified by POSIX.
50+
51+
# Where `SIGSTOP` is the standard signal name.
52+
53+
# Also possible with the XSI extension:
54+
55+
kill -SIGSTOP $ID
56+
kill -sigstop $ID
57+
kill -STOP $ID
58+
kill -stop $ID
59+
60+
# But not recommended because it is less uniform parameter passing,
61+
# and not guaranteed to be on all implementations.

killall.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Send signals to all process by name
2+
3+
# On Ubuntu, comes from the psmisc package.
4+
5+
# Application: firefox/skype hanged. `ps -A | grep -i firef',
6+
# confirm that the name is firefox and that it is the only one with that name, and then:
7+
8+
killall firefox
9+
10+
# This sengs SIGTERM, which programs may be programmed to handle,
11+
# so the progrma may still hang ( and in theory be trying to finish nicelly, although in practice this never happens... )
12+
13+
# Kill it without mercy:
14+
15+
killall -s 2
16+
17+
# which sends SIGINT, which processes cannot handle, so they die.

0 commit comments

Comments
 (0)