UNIX Commands --------------------------------- Background: The Shell ---------------------- A user-level program that displays a prompt then waits for user input Tries to execute the first word It applies any wildcard characters to files in the current directory It passes the rest of the line to the executing program ls ----------- LiSt files Syntax: ls [-l] [file(s)] Examples ls (displays the current directory) ls project99.c inputValues.txt rubric.txt (displays the specified files) ls -l project99.c inputValues.txt rubric.txt (displays the Long listing for specified files) ls project* (displays all of the files that start with project) ls *.c (displays all of the files that end in .c) ls project*.c (displays all of the files that start with project and end with .c) ls project[123].c (displays project1.c project2.c and project3.c) cp ----------- Copy a file to a new location Syntax: cp source_file_name destination_file_name Examples: cp inclassExample.c project99.c (creates a new file named project99.c based on an existing file, inclassExample.c) cp project1*.* project1/. (copy all files that start with project1 then have 0+ characters, then have a ".", then have 0+ characters into the directory named project1) mv ----------- Move a file to a new location Syntax: mv source_file_name destination_file_name Examples: mv inclassExample.c project99.c (Renames inclassExample.c to be project99.c (and potentially overwrites any existing project99.c) mv project1*.* project1/. (Moves all files that start with project1 then have 0+ characters, then have a ".", then have 0+ characters into the directory named project1) rm ----------- Remove/delete the specified file(s) Syntax: rm [-r] filename [filename(s)] Examples: rm tempFile.txt (deletes tempFile.txt) rm -r junkDirectory/ (deletes junkDirectory and everything in it (recursively)) (Use with extreme caution) head ----------- Display the first n lines from the specified file(s) or standard input (tail displays the last n lines) Syntax: head [-n num_lines] [filename(s)] Examples: $ head helloWorld-fork.c #include #include #include #include #include int globalInt = -1; int main(int argc, char* argv[]){ pid_t pid; // Process ID (displays the first 10 lines of helloWorld-fork.c) $ head -n 1 helloWorld-fork.c #include (displays the first line of helloWorld-fork.c) cat ----------- ConCATenate the contents of the specified file(s) or standard input Syntax: cat [filename(s)] Examples: cat fruits.txt veggies.txt (displays the contents of fruits.txt followed immediately by veggies.txt) cat < fruits.txt (takes the contents of fruits.txt and redirects it to standard input, which is then feed to cat, which displays the contents of the files) (same result as cat fruits.txt) sort ----------- Sorts the contents from the specified file(s) or standard input Syntax: sort [filename(s)] Examples: sort fruits.txt veggies.txt (sorts the combined contents of fruits.txt and veggies.txt) cat fruits.txt veggies.txt | sort (sorts the combined contents of fruits.txt and veggies.txt) grep ----------- Display lines matching a specified pattern Syntax: grep [-r] [-i] [filename(s)] Examples: $ grep -i berr fruits.txt strawberries raspberries blueberries black berries (displays all of the lines in fruits.txt that have "berr" somewhere in the line; -i makes the search case-Insensitive) wc ----------- Display the number of characters, words and lines in the specified file(s) or standard input Syntax: wc [-c] [-l] [filename(s)] Examples: $ wc -l fruits.txt 11 fruits.txt (displays that there are 11 lines in fruits.txt) $ grep -i berr fruits.txt | wc -l 4 (Gets all of the lines with "berr" somewhere in them, sends that to standard out, wc takes it as standard input and counts that there are 4 lines) chmod ----------- Change file permissions for user, group and/or anyone Syntax: chmod [-R] mode filename(s) mode: specifies which permissions to set/unset Examples: chmod 700 projects/ or chmod u=rwx,go-rwx projects/ (make the projects directory readable, writable and executable by the user and not readable, writable nor executable by the group nor everyone else) chmod -R 700 projects/ (make the projects directory (and Recursively everything in it) readable, writable and executable by the user and not readable, writable nor executable by the group nor everyone else) mkdir ----------- Make a directory Syntax: mkdir directory_name(s) Examples: mkdir project99 or mkdir project99/ (creates a directory names project99) rmdir ----------- Make a directory Syntax: mkdir directory_name(s) Examples: mkdir project99 or mkdir project99/ (creates a directory names project99) cd ----------- Change (working) directory Syntax: cd Syntax: cd directory_path Examples: cd project99 (now the current working directory is project99) cd cpsc3125/projects/project99 (now the current working directory is project99, which is inside of the cpsc3125/projects directory) cd .. (Goes to the parent directory) cd (changes the current working directory to $HOME) ln ----------- Create a hard or symbolic link Syntax: ln source_file_name destination_file_name Syntax: ln -s source_file_name destination_file_name Examples: ln backupYesterday/project99.c backupToday/project99.c (Creates a hard link (or an alias) to backupYesterday/project99.c named backupToday/project99.c) (Changes made to either file will be reflected in the other file) ln -s /mnt/c/Users/superman/school/cpsc3125/project1 proj1WindowsDir (Creates a symbolic link to the project1 directory named proj1WindowsDir) (The contents of project1 and proj1WindowsDir are always the same. proj1WindowsDir is a "shortcut" to project1) (Note: It's possible to make symbolic links to files as well as directories)