Skip to content

Instantly share code, notes, and snippets.

@pixelstorm
Forked from molcay/Linux CLI Commands.md
Last active April 14, 2019 06:25
Show Gist options
  • Save pixelstorm/1e930efb636529ac4af74f46fd37dcda to your computer and use it in GitHub Desktop.
Save pixelstorm/1e930efb636529ac4af74f46fd37dcda to your computer and use it in GitHub Desktop.
Linux Commands
  • cat: concatenate files and print on the standard output
    • cat > a.txt: start a file creation
    • cat a.txt: show file content
    • cat a.txt b.txt: show files content respectively
    • cat a.txt b.txt > c.txt: create a file which contains a.txt and b.txt
  • chmod: change file mode bits
    • chmod +x a.txt: All users and groups have the execute permission
    • chmod $u$g$o a.txt: Give specific user permission
      • $u -> current user permission number
      • $g -> current user's group permission number
      • $u -> others permission number
      • permission number can calculate with:
        • 0 -> no permission
        • 1 -> x (execute)
        • 2 -> w (write)
        • 4 -> r (read)
    • chmod 651 a.txt: it means user can write and read, group can read and execute, others can only execute.
  • cd: change directory
    • cd a/: change directory to a (which is a directory in my current directory)
    • cd /opt/: change directory to opt (full path start with /)
    • cd ../:one directory up
    • cd ./: this directory
    • cd: return home directory for your user
  • cp: copy files and directories
    • cp $source $destination: copy the file from source to destionation
    • cp -R a/ b/: copy directory(folder) to other folder. (-R = -r = --recursive)
  • date: print or set the system date and time
  • echo: display a line of text, args etc.
    • echo "string": it prints out "string"
    • echo $PATH: it prints $PATH variable's value
  • grep: print lines matching a pattern
    • cat b.txt | grep q: the lines which contains q in b.txt.
    • cat b.txt | grep q$: the lines which ends with q in b.txt.
    • cat b.txt | grep ^q: the lines which starts with q in b.txt.
    • ! for regex usage you can only specify the -E(or -e): cat b.txt | grep -E q$.
  • head: output the first part of files
    • head b.txt: first 10 lines of the b.txt (default)
    • head b.txt -n 4: first 4 lines of the b.txt
  • ls: list directory contents
    • ls: list current directory contents (only general files ignore hidden files(which starts with .))
    • ls a/: list directory a's contents
    • ls -l: list current directory contents (long list format)
    • ls -a: list current directory all contents (do not ignore hidden files)
    • ls -la: all file in long list format
  • more: file perusal filter for crt viewing
    • more b.txt: paginate the contents of the b.txt
    • cat b.txt | more: this also paginates
  • less: opposite of more
  • mkdir: make directories
    • mkdir NEWFOLDERNAME: create a folder(directory) name NEWFOLDERNAME
  • mv: move (rename) files
    • mv $source $destination: move folder and files from source to destination (you can use it to rename a folder or file)
  • pwd: print name of current/working directory
  • rm: remove files or directories
    • rm b.txt: delete file named b.txt
    • rm -r a/: delete folder named a
  • rmdir: remove empty directories
    • rmdir a/: delete directory if and only if the directory is empty
  • export: set an environment variable
    • export VARIABLE_NAME="VALUE OF VARIABLE": it creates a variable named VARIABLE_NAME. (access name $VARIABLE_NAME)
  • sort: sort lines of text filesq
  • tail: output the last part of files
    • tail b.txt: last 10 lines of the b.txt (default)
    • tail -n 4 b.txt: last 4 lines of the b.txt
  • tar: The GNU version of the tar archiving utility (tape archive)
    • tar -tvzf foo.tar.gz: list all files name in foo.tar.gz
    • tar -zcvf archive-name.tar.gz directory-name: create a tar.gz file from directory-name
    • tar -xvzf foo.tar.gz: uncompress all files in foo.tar.gz
      • -z: using gzip program
      • -x: extract (uncompress)
      • -c: create archive
      • -v: verbose (display progress while creating archive)
      • -f: archive file name
  • ssh: OpenSSH SSH client (remote login program)
    • ssh eng1.mu.edu.tr: connect the eng1.mu.edu.tr server with SSH (it get your current username as a username in the server).
    • ssh mo.tercanli12@eng1.mu.edu.tr: connect the server with username: mo.tercanli12
  • wc: print newline, word, and byte counts for each file
    • wc b.txt: prints out the number of Lines, Words, Characters
    • wc -l b.txt: only number of lines
    • wc -w b.txt: only number of words
    • wc -c b.txt: only number of character
  • scp: secure copy (remote file copy program)
    • scp your_username@remotehost.edu:foobar.txt /some/local/directory: copy file from remote to local
    • scp foobar.txt your_username@remotehost.edu:/some/remote/directory: copy file from local to remote
  • wget: The non-interactive network downloader.
    • wget $URL: download url content from $URL
    • wget $URL -O a.txt: download url content from $URL and save it as a.txt
  • curl: transfer a URL
  • find: search for files in a directory hierarchy
  • man: an interface to the on-line reference manuals
    • man tar: manual page for tar
  • sed: stream editor for filtering and transforming text
    • sed s/one/ONE/ < a.txt: it process the file a.txt and all one become ONE. (a.txt does not change)
  • awk: pattern scanning and processing language

//copy files from text list to folder function cp_files_from_text_list_to_folder() { TEXTLIST=$1 DESTINATION=$1 cat $TEXTLIST | xargs -J % cp % $DESTINATION }

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