User Tools

Site Tools


linux

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux [2021/01/22 17:03] rogerlinux [2023/01/11 10:33] (current) – [Print the first column of a string] roger
Line 3: Line 3:
 Useful Linux commands, with no particular order Useful Linux commands, with no particular order
  
-==== Check which program is using a particular port ====+===== Check which program is using a particular port =====
      
 <code bash>sudo lsof -i:port</code> <code bash>sudo lsof -i:port</code>
  
-==== Kill a particular PID ====+===== Kill a particular PID =====
  
 <code bash>sudo kill -9 PID</code> <code bash>sudo kill -9 PID</code>
  
-==== Check the current folder size ====+===== Check the current folder size =====
  
 <code bash>du -hs .</code> <code bash>du -hs .</code>
  
-==== Show filesystem information ====+===== Show filesystem information =====
  
 <code bash>df -h</code> <code bash>df -h</code>
  
-==== SSH ====+===== SSH =====
 More info: https://www.ssh.com/ssh/tunneling/example/ More info: https://www.ssh.com/ssh/tunneling/example/
  
-=== Copying local keys to a remote server ===+==== Copying local keys to a remote server ====
  
 <code bash>ssh-copy-id user@server</code> <code bash>ssh-copy-id user@server</code>
  
-=== Create a remote tunnel into localhost ===+==== Create a remote tunnel into localhost ====
  
 <code bash>ssh -L local-port:localhost:remote-port user@server -N</code> <code bash>ssh -L local-port:localhost:remote-port user@server -N</code>
Line 32: Line 32:
 Note: ''AllowTcpForwarding'' and ''PermitOpen'' have to be enabled in ''/etc/ssh/sshd_config'' Note: ''AllowTcpForwarding'' and ''PermitOpen'' have to be enabled in ''/etc/ssh/sshd_config''
  
- +==== Forward a local port to a remote host (like ngrok) ====
-=== Forward a local port to a remote host (like ngrok) ===+
  
 <code bash>ssh -N -T -R local-port:localhost:remote-port user@server</code> <code bash>ssh -N -T -R local-port:localhost:remote-port user@server</code>
Line 41: Line 40:
   * -R      Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.   * -R      Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the local side.
  
-=== SCP ===+==== SCP ====
  
 <code bash>scp -r /folder/to/upload user@server:/destination/on/server</code> <code bash>scp -r /folder/to/upload user@server:/destination/on/server</code>
  
-==== tar & untar ====+===== tar & untar =====
  
-=== tar ===+==== tar ====
  
 <code bash>tar -czvf file.tar.gz file/or/folder/to/tar/</code> <code bash>tar -czvf file.tar.gz file/or/folder/to/tar/</code>
Line 56: Line 55:
   * -f filename   * -f filename
  
-=== untar ===+==== untar ====
  
 <code bash>tar -xvf file.tar.gz</code> <code bash>tar -xvf file.tar.gz</code>
Line 64: Line 63:
   * -f filename   * -f filename
  
-==== gpg encrypt/decrypt ====+===== gpg encrypt/decrypt =====
  
-=== Encrypt ===+==== Encrypt ====
  
 <code bash>gpg -c --cipher-algo AES256 file/to/encrypt</code> <code bash>gpg -c --cipher-algo AES256 file/to/encrypt</code>
Line 72: Line 71:
 You will be asked to enter a password You will be asked to enter a password
  
-=== Decrypt ===+==== Decrypt ====
  
 <code bash>gpg file/to/decrypt</code> <code bash>gpg file/to/decrypt</code>
  
 You will be asked to enter a password You will be asked to enter a password
 +
 +===== Download an entire website using wget =====
 +
 +More: https://gist.github.com/mikecrittenden/fe02c59fed1aeebd0a9697cf7e9f5c0c
 +
 +<code bash>
 +wget \
 +     --mirror \ # Makes (among other things) the download recursive.
 +     --page-requisites \ # Get all assets/elements (CSS/JS/images).
 +     --adjust-extension \ # Save files with .html on the end.
 +     --span-hosts \ # Include necessary assets from offsite as well.
 +     --convert-links \ # Update links to still work in the static version.
 +     --restrict-file-names=windows \ # Modify filenames to work in Windows as well.
 +     --domains yoursite.com \ # Do not follow links outside this domain.
 +     --no-parent \ # Don't follow links outside the directory you pass in.
 +         yoursite.com/whatever/path # The URL to download
 +</code>
 +
 +===== Create a X.509 sha256 self signed certificate =====
 +
 +<code bash>
 +openssl req \
 +  -x509 \
 +  -newkey rsa:4096 \
 +  -sha256 \
 +  -keyout mykeyname.key \
 +  -out mycertname.pem \ 
 +  -days 365 
 +  -nodes # only if you need no password
 +</code>
 +
 +===== Find =====
 +
 +==== Find a specific file in the specified folder ====
 +
 +You can use:
 +
 +<code bash>
 +find mypath -type f -name "myfile.extension"
 +</code>
 +
 +  * ''mypath'': a path in the OS to perform the search.
 +  * ''-type'': type of file to look for. Most common is ''f'', which means "Regular file".
 +  * ''-name'': the name of the file you are looking for. You can also use wildcards, for example: ''*.json'' to find all the JSON files in the current directory.
 +
 +===== xargs =====
 +
 +''xargs'' is used to create new commands from the output of another command. For example, if I'm performing a ''find'' command, I could use ''xargs'' to issue a new command for each line of the output of ''find''.
 +
 +<code bash>
 +find . -type f -name \"*.json\" | xargs --verbose -I % sh -c 'cat % | jq -c || exit 255'
 +</code>
 +
 +What's happening?
 +
 +  * First, we are performing a [[linux#find|find]] command. The output is going to look like this:
 +<code bash>
 +./folder/this_is_a_json.json
 +./another.json
 +</code>
 +  * Then, we pipe the output of the find command to ''xargs''. The ''-I %'' means "replace string", so ''xargs'' is going to replace any ''%'' it finds with the value of the current line it is processing.
 +  * ''xargs'' is going to run ''sh -c "cat % | jq -c || exit 255"'' (remember its going to replace ''%'' with the value of the current line it is processing) on each of the lines resulting from the ''find'' command. ''jq'' is a program that is used to format JSON. If ''jq'' fails (eg, the JSON is malformed), it's going to return ''exit 255'', to stop the execution of ''xargs''
 +
 +
 +This command in particular is very usefull to check if all the JSON files in a repository are well formated in a CI/CD step.
 +
 +===== ncdu =====
 +
 +Ncdu is a disk usage analyzer with an ncurses interface. It is designed to find space hogs on a remote server where you don’t have an entire graphical setup available, but it is a useful tool even on regular desktop systems. Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed.
 +
 +Usage:
 +
 +<code bash>
 +ncdu -x /
 +</code>
 +
 +Where ''/'' is the filesystem you want to check
 +
 +===== awk =====
 +
 +==== Print the first column of a string ====
 +
 +<code bash>
 +echo "This is a string" | awk '{print ($1)}'
 +# Output: "This"
 +</code>
 +
 +==== Print a full string in lowercase or uppercase ====
 +
 +<code bash>
 +echo "This Is A CaPiTaLiZeD String" | awk '{print tolower($0)}'
 +# Output: "this is a capitalized string"
 +
 +echo "This Is A CaPiTaLiZeD String" | awk '{print toupper($0)}'
 +# Output: "THIS IS A CAPITALIZED STRING"
 +</code>
linux.1611345835.txt.gz · Last modified: 2023/01/01 18:43 (external edit)