This document provides a summary of common Unix commands and their functions. It lists commands for navigating directories, viewing file contents, copying/moving files, editing text, searching for text, managing permissions, and more. Pipes, redirection, and wildcards can be used to string commands together or select specific files as arguments.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
35 views
Unix Command List
This document provides a summary of common Unix commands and their functions. It lists commands for navigating directories, viewing file contents, copying/moving files, editing text, searching for text, managing permissions, and more. Pipes, redirection, and wildcards can be used to string commands together or select specific files as arguments.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Unix Command List
pwd print current working directory
file determines type of file & its data cd changes current working directory ls used to list the contents of a directory ls -a list all files, including any dot files ls -F indicates file types: / = dir. | * = executable ls -m show files as a comma-separated list ls -s show size of files, in blocks(typically, 1 block = 1024 bytes) ls -C force multiple-column output on listings ls -1 force single-column output on listings ls – l show long-listing format, including permissions, ownership, size, and date (.) referenced as the current working directory (. .) referenced as the parent directory or back once mkdir will make a new directory (~) known as the home directory mkdir -p multiple new directories at once. - Ex: mkdir–p c/b/a tree shows the current directory & its subdirectories cp copies one or more files more can also be used to examine the contents of a file -space bar: page forward -b: move backwards -q: quit |more tame commands/allows to page through text one page at a time. - ‘|’ known as the pipe in order to output of one command into the input of another -Piping ex: -myprog | grep “ERROR:” | more /bin where most of the basic Unix commands are mv move and/or rename a file rmdir deletes directories (directory must be empty) rm removes files man looks up the manual page for the given command man -k looks up the given keyword in an index and lists the commands that may be relevant chmod change permissions of files & directories ex: chmod permissions files Permission arguments: 1. Category of permission you want to change: *more than one category ex: ‘go’ or ‘all’ = ‘ugo’ - owner’s permission(user): u - group’s permission: g - other’s permission: o 2. What you would like to do with permission: - add permission: + - delete permission: - - specify it exactly: = 3. What permissions you want to affect: -read: r/write: w/execute: x chmod a-w protects a file from accidental editing ex: chmod a-w filename chmod u+w you own an unwritable file that you want to edit, but you want to change other people’s write permissions chmod u-w delete your own permission chmod a-w delete everyone’s write permission env lists all environment & shell variables active in your current process echo prints whatever parameters it is given who see who is currently logged into the same machine as you grep searches for lines containing desired text -c: list a count of matching lines only -i: ignore the case of the letters in the pattern -l: list only the names of files that contain the specified pattern -n: include line numbers -v: show lines that don’t match the specified pattern **great for editing & selecting text** diff will check if text files are similar (Copy) ctrl + shift + 0 (Paste) shift + 0 nano a text editor wc(word count) reads text from its input stream & produces as its outputs stream 3 #’s indicating the # of lines, words, characters that it saw focus wc command with: -c/-w/-1 ex: wc < hello.c sed(stream editor) scan each line of input for a pattern & to replace that pattern, wherever it occurs, by some string. -sed s/pattern/replacement/g -pattern: describes text to search for in each line - replacement: text we want to use to replace the text matching the pattern -g: indicates the change should take place every time.(no ‘g’ only the first match is applied). - the ‘/’ can be any special character i.e.: @$%* -can add flag ‘i’ to get rid of case-sensitive default -e option allows sed to string together multiple substitution commands quoting ( \ ) – backslash place in front of the special character ex: grep \* foo.txt (can quote themselves) (single quotes(‘ ‘) – quoting) can enclose all or part of the argument in single quotation marks -suppresses all characters -combines multiple parameters into a single parameter (double quotes(“ “) – quoting) suppresses all special characters except ‘$’ & also gather its contents into a single parameter -treats $USER as a command in double quotes (input redirection) use <: operates to make wc command accept a file as input (output redirection) >: redirects the standard output into a file - wc < hello.c > hello.wc -use >> if we want to add output at the end of an existing file instead of replacing that file -ex: wc < hello.c >> hello.wc (pipes ( | )) sends the output of one command into the input of another command. -common ex of a pipe is: | more (Backticks ( ` ` ) – to left of the “1” key) the shell treats enclosed characters as a command & replace the whole `-surrounded string by the output of the command` -opposite of “Quoting” -ex: echo Today is `date` . xargs reads a list of file names from the standard input & fills those file names into a command of your choosing ex: xargs partial-command - ‘-n’: controls the maximum # of files the xargs will pack into one command - ‘-i’: looks in your partial command for the characters “{}” & places your files there.(one at a time as if you used “-n 1” - ex: ls /bin | xargs -i echo Hello {} World find provides all kinds of ways to select files & provides a variety of things it can do with selected files -general form: - find list-of-files-&-directories list-of-actions - find looks at each file and dir. given in the list - for dir. it also looks at all files & dirs. inside those, descending as far as it can from dir. to dir. - the actions in the command are all given a flag (-). Some actions will “do something” to a file. Others are used to select which files will be passed on to the later commands in the list. - ‘-name’: given a wildcard expression to match file names against -ex: find /usr/include -name ‘w*.h’ - ‘-type’: another way to select files & chooses different types of files. Dir. are type ‘d’ & ordinary files are type ‘f’. -ex: find /usr/include -type d -name ‘u*’ -can also select files based on how long ago modified: ex: -mtime +7(more than 7 days)|-mtime -7(less than 7 days) - ‘-print’: is the default option if you do nothing to selected files. May need to use if you want to print a file name & do something else to a file - ‘-exec’: allows you to specify an arbitrary Unix command that you want applied to selected files -ex: find ~/xargs -name ‘*.bak’ -print -exec rm {} \; - the ex. above prints a filename before passing it on to the command in the -exec action - ‘-ok’: asks for permission before applying a command cat dumps the contents of one or more text files to your screen head command expects to find a number - ‘-n’: (shell variable) typically begins with an alphabetic character. When we want to get a value a shell variable, we add ‘$’ to the front of it. - ex: give command: A=hello - next command: echo $A - can integrate strings as #’s: - ex: cat 1.txt - ex: one=1 - ex(cont.): head $one.txt (shell variables & backticks) backticks used to insert the output of one command into another, which is exploited to assign environmental variables - ex: Today=`date` - ex(cont): echo $Today - *we can also use pipes within backticks to alter the values that would be stored in a variable* basename (filepath) prints the last part of a path, just after the final / - ex: basename ~/playing dirname (filepath) extracts the part of a path just before the final /, or prints . if the path has no /’s - ex: dirname . readlink -f (filepath) prints the “canonical form” of the file path, which is the absolute path to the file with no unnecessary steps. - not only turns relative paths into absolute paths, it “canonicalizes” the path by getting rid of steps like “../” or “./”. sh or csh starts another copy of the shell currently running & runs as a separate process(the old one s temporarily suspended) export shell variable value will be seen by child processes. done with the sh & bash commands. - will export a shell variable to other shell. $PATH(an env. variable) determines what programs you can execute without typing in a full path name - ex: you compiled C++ program names “yourProgram” into your current working directory. - ex(cont.): Some people can execute the program: yourProgram -ex(cont.): others like this: ./yourProgram - depends on if your account has been set up so that your cwd (.) is in your $PATH - if it is use the first form - if not use the second form - used for special directories full of customized & frequently-used programs. ps used to display the attributes of processes that are running currently. options: -a (all users) -f (full list) -u (user) -t (terminal) -e (every) file (Linux) this is the best way to see what kind of data is in a file - prints a description of the file contents - (In Windows, the best way to see if a file is text or binary is to open it in a text editor such as notepad) converting a Windows-style text file to Linux - use the command “tr” converting Linux to Windows use ‘unix2dos’ -ex: unixdos file1 SFTP (Secure File Transfer Protocol) text-based client is launched: -ex: sftp cs_tmcle012@linux.cs.odu.edu
get (Windows) downloads a file from remote machine onto the
local machine put (Windows) uploads a file from your local machine to the remote machine quit exit also works File Permissions begins with: ‘-‘ = a file ‘d’ = directory -r: read -w: write -x: execute Groups: -u: user (group 1) -g: group (group 2) -o: other people(group 3) 3different Groups (drwxrwxr-x): first group: the owner permissions second group: the group permissions third group: Everyone else permissions ex (if we want to change permissions): chmod o+w file o(ex): states what group we want to change permission for +w(ex): states what permission we would like to add to this group (group ‘o’ in this case) chmod o-w o(ex2): states the group we want to change permissions for -w(ex2): states to take away the write permission for the outside people. Which us group 3 To set all groups permission at once: ex: chmod 754 File 7, 5, 4 represents the induvial permissions for(in this order) user, group, other 4 – stands for “read” (allows to do ls) 2 – stands for “write” 1 – stands for “execute” (access files) 0 – stands for “no permission” (will be labeled as a ‘-‘) -chmod: means to change permissions emacs (How to compile code) in order to compile code we must: - press esc+x -next type ‘compile’ then press enter - next compiler we use and source file: gcc sourcefile code . - will open up folder on VSCODE ctrl + ~ - this will open the terminal on VSCODE ./ - this command allows us to test our cpp compilations. - ex: ./dividers