Some useful terminal commands in Linux or OSX
-> List all cronjobs from all users and services (all existing)
1
| for user in $(cut -f1 d: /etc/passwd); do crontab -u $user -l; done
|
-> For all users and services in /etc/passwd
1
| for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
|
-> A nicer list for each user in /etc/passwd
-> Find and delete 10 days old files in /tmp
1
| find /tmp -ctime +10 -exec rm -rf {} +
|
-> Zip and Tar over SSH compression
Compresses/packs the archiveName from any directory or file to a new file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # dir with zip (you might need to install this)
zip -r archiveName.zip /directory/to/compress
# decompress (extracts archiveName's content to the current folder (in place))
unzip archiveName.zip
# dir with tar
tar -cvf archive_name.tar /directory/to/compress
# decompress (extracts archiveName's content to the folder '/tmp')
tar -xvf archiveName.tar -C /tmp
# dir with tar and gzip compression
tar -czvf archiveName.tar.gz /path/to/dirOrFile
# decompress (extracts archiveName's content to the folder '/tmp')
tar -xzvf archiveName.tar.gz -C /tmp
# dir with tar and bzip2 compression
tar -jcvf archiveName.tar.bz2 /directory/to/compress
# decompress (extracts archiveName's content to the folder '/tmp')
tar -jzvf archiveName.tar.bz2 -C /tmp
|
-> Shop open ports
-> Show usage of port $NUMBER
here TCP protocol with port 9000
1
| lsof -nP -i4TCP:9000 |grep LISTEN
|
-> Search | Find files
Where you are
Or in a different, named folder
1
| find /var/log -name "*nginx*"
|
-> Chmod - Change permissions
1
2
3
4
5
6
7
8
| d rwx r-x r-x 6 User staff 204 17 Mai 15:30 vendor
^ ^^^ ^^^^----^^^^^
| ||| |||| |||||
| ||| -> belongs to "User" in the user-group "staff"
| ||| | || -> right to execute
| || | | -> right to write
| | | -> right to read
| -> directory
|
actually change:
1
2
3
4
5
| chmod [-v] [-R [-L | -P]] mode file ...
verbose
recursive
[-L: symbolic links are followed]
[-P: don't follow symbolic links (default)]
|
Overview for modes / bitmatrix:
| inputvalue | bitwise | shownrights | meaning |
|---|
| 0 | 000 | — | no access |
| 1 | 001 | –x | execute |
| 2 | 010 | -w- | write |
| 3 | 011 | -wx | write / execute |
| 4 | 100 | r– | read |
| 5 | 101 | r-x | read / execute |
| 6 | 110 | rw- | read / write |
| 7 | 111 | rwx | read / write / execute |
Now set all files inside this folder to d - rwx r-x r--:
1
| user@computer $ chmod -R 754 vendor/
|
-> Chown - Change file | folder ownage
1
2
3
| drwxr-xr-x 3 root staff 102 25 Mai 2016 .vim
^^^^--^^^^^ |||| |||||
-> belongs to user "root" in the user-group "staff"
|
Syntax for change-ownage ‘chown’:
1
2
3
4
| chown [-v] [-R [-L | -P]] owner[:group] file ...
- See 'chown' above for declaration of arguments,
- owner means the users account name
- group is optional and means the name of 'his' user group
|
Now I want to change this belonging recursively to me, user:staff (with user’s group!):
1
2
3
4
5
| User@computer /folder/where/vim/is $ su (or) sudo su
root@computer / # cd /folder/where/vim/is
root@computer /folder/where/vim/is # chown -R user:staff .vim
root@computer /folder/where/vim/is # [CTRL+D]
User@computer /folder/where/vim/is $ exit
|