Skip to content

Instantly share code, notes, and snippets.

@francisco-rojas
Last active March 14, 2017 13:42
Show Gist options
  • Save francisco-rojas/700d5822b447910d97bd to your computer and use it in GitHub Desktop.
Save francisco-rojas/700d5822b447910d97bd to your computer and use it in GitHub Desktop.
Linux

###du Summarize disk usage of each FILE, recursively for directories sorted by size

sudo du -x -h / | sort -n | tail -40

###truncate Truncate files to size 0MB

truncate -s 0 production.log

###find To fine files larger than 10MB

sudo find / -type f -size +10M -exec ls -lh {} \;

###lsof To find log files for a running process

# shows all open files:

root@lnx-work:~# lsof 
...

# shows all open files that contain the string "log" in the filename:

root@lnx-work:~# lsof | grep -e log 
...
cupsd     13279             root    4u      REG              252,1     2086     394345 /var/log/cups/access_log
httpd     18971 root    2w   REG             253,15     2278      6723 /var/log/httpd/error_log
httpd     18971 root   12w   REG             253,15        0      1387 /var/log/httpd/access_log
...

# shows all open files that contain the string "log" and "access" in the filename (to find apache access logs) :

root@lnx-work:~# lsof | grep log | grep access
...
cupsd     13279             root    4u      REG              252,1     2086     394345 /var/log/cups/access_log
httpd     18971 root   12w   REG             253,15        0      1387 /var/log/httpd/access_log
...

Use PID to filter for open files by process

# ps axu |grep httpd
- snip -
root     18971  0.0  0.3 495964 64388 ?        Ssl  Jan10   4:47 /usr/sbin/httpd
- snip -

Search open logfiles using lsof and the PID (and grep for “log”).

# lsof -p 18971 |grep log
httpd   18971 root    2w   REG             253,15     2278      6723 /var/log/httpd/error_log
httpd   18971 root   12w   REG             253,15        0      1387 /var/log/httpd/access_log

Reference: http://slash4.net/blog/howto/find-log-file-location-of-linux-process-with-lsof.html

###chmod To change all the directories to 755 (-rwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

###tail To view real time changes on a file:

tail -f /path/to/file.txt

To view the last # of lines:

tail -n 500 /path/to/file.txt

###curl To submit a GET action (curl's default action) to a url

curl http://api.cs-zombies-dev.com:3000/zombies

To submit a GET action with query string parameters

curl http://api.cs-zombies-dev.com:3000/zombies?weapon=axe

Use the -I option to only display response headers

curl -I http://api.cs-zombies-dev.com:3000/zombies/7

Send custom request headers with the -H option

curl -IH "Accept: application/json" localhost:3000/zombies

The -X option specifies the method

curl -i -X POST -d 'episode[title]=ZombieApocalypseNow' \ 
http://localhost:3000/episodes

(you can use a backslash to get the shell to ignore a newline character. This way allows us to embed newlines in our command)

To submit a POST action to a url with json parameters and providing user credentials

curl -X POST -u username:password --header "Content-Type:application/json" -d '{"from":"Story","select":["Name","Number","Scope.Name"],"where":["Scope":"$myScope"],"with":["$myScope":"Scope:1111"]}' https://www12.v1host.com/domain/query.v1

Curl has built-in support for authenticated HTTP requests.

curl -Iu 'carlos:secret' http://localhost:3000/episodes

curl -IH "Accept: application/json" \ 
-u 'carlos:fakesecret' http://localhost:3000/episodes

you can also send Basic Auth credentials as part of the URL (Basic Auth is simple , but not secure)

curl -I http://carlos:secret@localhost:3000/episodes

###scp To upload files to the server:

scp /file/path user@server.ip:/destination/path/

To download files from the server:

scp user@server.ip:/source/path/to/file.txt /destination/path/locally

###ln To create symlinks

ln -s /path/to/file /path/to/symlink

###grep Find the given text in the file and print the 10 lines after and before each ocurrence.

grep -C 10 "EOF" file.log

###usermod Adding an existing user to a new group.

sudo usermod -a -G groupName userName

###htop An interactive process viewer for Linux. It is a text-mode application

htop

###ps The ps command on linux is one of the most basic commands for viewing the processes running on the system.

# Display all processes
$ ps ax
$ ps -ef

#Use the "u" option or "-f" option to display detailed information about the processes
$ ps aux
$ ps -ef -f

#Display process by user
$ ps -f -u www-data

#Show process by name or process id
$ ps -C apache2

see: http://www.binarytides.com/linux-ps-command/

###netstat Netstat is a command line utility that can be used to list out all the network (socket) connections on a system

# List all connections
netstat -a

netstat -anp

see: http://www.binarytides.com/linux-netstat-command-examples/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment