Some Useful Linux Commands ========================== ----- Start of keyboard shortcut commands for editing the bash command line --------------------------------------------------------------------------- Use arrow keys to move one character backward/forward or one line up/down -Moving by line and by word CTRL + A move cursor to beginning of line CTRL + E move cursor to end of line ALT + B move one word backward to the first non-space in previous word ALT + F move one word forward to first space beyond current word -Case conversion ALT + U uppercase from current character to end of word and move beyond word ALT + L lowercase from current character to end of word and move beyond word ALT + C uppercase current character and move beyond word -Swapping characters and words CTRL + T swap current and previous characters ALT + T swap current and previous words -Deleting words and (portions of) lines and undeleting/undoing ALT + Backspace Delete from before current character to beginning of word ALT + D Delete from current character to end of word CTRL + W Cut word before cursor to clipboard CTRL + U delete from character before cursor to beginning of line CTRL + K delete from current character to end of line CTRL + Y paste (yank) last thing to be cut -Miscellaneous CTRL + _ undo the last action CTRL + L clear screen and copy current command line to the top CTRL + R search through command history ALT + R revert changes and replace line as it was in history ------------------------------------------------------------------------- ----- End of keyboard shortcut commands for editing the bash command line -- To change your password: passwd -- To see what's in the current directory: ls -- To see what's in a subdirectory of the current directory: ls subdir_name -- To change directory to a particular subdirectory: cd subdir_name -- To change back to the previous directory: cd - -- To change to the parent directory: cd .. -- To change to your home directory: cd -- To see what directory you're in (the "current working directory"): pwd -- To easily repeat a previous command (or modify it and then repeat) history (this shows you recent commands, all numbered) !1234 (if you want to rerun command number 1234 enter this on the command line) and press ENTER) -- But ... if you want to edit command number 1234 before running, perform the following two steps after the history command shows you command numbers: First ... !1234 (enter this on the command line BUT DO NOT PRESS ENTER Second ... Ctrl + Alt + E (puts command 1234 on command line, after which you can just press Enter, or edit and then press Enter) -- To copy a file: cp old_file new_file -- To copy a file to a new location: cp file path_to_new_location/ -- To copy a file to a new location with a new name: cp file path_to_new_location/new_name -- To recursively copy a directory and its contents to the current directory: cp -r path_to_directory_to_be_copied/ . -- To recursively copy a directory and its contents to a destination directory: cp -R source_path/source destination_path/ after which destination_path will contain a complete copy of source. -- To make a copy of one version of your website to a new version and preserve file permissions: cp -rp old_version new_version -- To make a new subdirectory in the current directory: mkdir new_subdirectory -- To make 3 new subdirectories in the current directory: mkdir {sub1,sub2,sub3} -- To make a new subdirectory when the parent directory does not exist: mkdir -p parent/subdirectory -- To remove a subdirectory (which must be empty): rmdir subdirectory_name -- To create a new (empty) file: touch filename -- To remove (delete) a file: rm filename -- To remove recursively all files (be very careful with this one): rm -R directory_name -- To display a file: cat filename -- To display a file one screen at a time: more filename -- To see the permissions of a file (or other entity) in numerical format: stat -c "%a %n" entity_name -- To see exactly what a echo command sees (for example): echo ABC | od -bc -- To search for a target word in a file grep target file grep target file1 file2 (to search more than one file) grep target * (to search all files in a directory) grep -w target file (to search for the whole word only) grep -i target file (to ignore case when searching) grep -r target * (to search current directory and all subdirectories) grep -v target file (to exclude all lines that contain the target) grep -x "some text here" * (to find all lines that match exactly) grep -l target * (to print only the filenames that match your search) grep -c target * (to count the number of matches) grep -A 3 target file (to also print three lines after the match) grep -B 3 target file (to also print three lines before the match) grep -C 3 target file (to also print three lines before and after the match) grep -n -C 2 target file (to print line numbers as well grep -m2 target file (to limit the number of matches to 2) grep -m2 target * (to get just the first two matches in every file) -- To grep for multiple strings, patterns or words The difference between grep and extended grep is that extended grep includes meta characters that were added later. These characters are the parenthesis (), curly brackets {}, and question mark. The pipe character | is also treated as a meta character in extended grep. [grep -E == egrep, which is deprecated] grep 'pattern1\|pattern2' fileName_or_filePath (Note the \ character.) grep -E 'pattern1|pattern2' fileName_or_filePath (Note: No \ character.) grep -e pattern1 -e pattern2 fileName_or_filePath You can also use -w, -i, -c, -R and * in ways analogous to the above greps. -- To see all line numbers of all occurrences of the (case-insensitive) string "target" in all files in the current directory and all of its subdirectories: grep -rin target * -- To use grep like above but excluding multiple kinds of files: grep -rni target * --exclude=\*.{sql,csv,mp4,zip} -- To find all files whose names end with a php extension, located in the current directory and any of its subdirectories. find . -name "*php" -- To find all files whose names end with a php extension, located in the current directory and all subdirectories to a maximum depth of 3 levels. find . -maxdepth 3 -name "*php" -- Search for text within multiple files. find ./ -type f -name "*.txt" -exec grep 'Geek' {} \; -- Find a particular file anywhere, and silence any permission errors. find / -name "Foo.txt" 2>/dev/null -- Better than ls for finding just directories. find ~/Public -type d -- For finding empty files. find ~ -type f -empty -- To check two files to see if they are same diff file1 file2 (shows the differences) diff -s file1 file2 (just reports identical files) diff -q file1 file2 (just reports different files) diff -y -W 30 file1 file2 (use side-by-side display in 30 columns) diff -y -W 30 --suppress-common-lines file1 file2 -- To show a schematic diagram of the directory structure and contents, starting with the current directory. tree -- To ignore a couple of directories when performing a tree command tree -I "some_directory|some_other_directory" -- To create a symlink to a file ln -s actual_file symlink -- To remove a symlink to a file unlink symlink rm symlink -- To edit a file use either of the following (vim only if you know vim): nano filename vim filename -- To create a zip file zip [options] zipfile files_list zip myfile.zip file1 file2 *txt (for example) zip -d filename.zip file.txt (removes file.txt) zip -u filename.zip file.txt (update, or add, file.txt) zip -m myfile.zip *.c (deletes all *.c files after zipping) zip -r filename.zip directory_name (zips a directory recursively) zip -r mydir.zip docs (for example) zip -x filename.zip file_to_be_excluded (exclude a file from the zip file) zip -v filename.zip file1.txt (output messages in "versose" mode) zip -v file1.zip *.c (for example) -- To unzip a zip file unzip -l zipfile (to see what's in the file) unzip zipfile (to extract the files in it, will be prompted if overwriting) unzip -o zipfile (overwrite existing files without prompting, use with caution) unzip -n zipfile (skip the extraction of a file that already exists) unzip '*.zip' (unzip multiple zip files ... note the single quotes) unzip -q zipfile (suppress messages in "quiet" mode unzip zipfile -d /path/to/directory) unzip zipfile -x file1-to-exclude file2-to-exclude unzip zipfile -x "*.git/*" unzip test.zip samples -d tmp (unzip just the zipped samples directory to tmp) unzip -d . zipfile path/to/zipped/file.txt (extract a single file with path) unzip -p zipfile path/to/zipped/file.txt >file.txt (extract just file, not meta data) -- To create a tar (archive) file tar cvjf tar_filename.tbz file_list (or wild card) tar --create --verbose --bzip2 --file tar_filename.tbz file_list (or wild card) -- To extract a tar (archive) file tar xf tar_filename.tbz -- Simplified creation, display, extraction of a tarfile tar -cf tar_file files_to_be_tarred tar -tf tar_file tar -xf tar_file -- How to set an alias of your own alias_name='command_for_which_you_want_the_alias' -- Two ways to check to see if you have any "orphan" tmux sessions: tmux list-sessions who | grep `whoami` | grep tmux -- One way to delete all of those sessions (if you don't want to recover them): tmux ls tmux a -t # tmux kill-server -t # -- Command to add to a script if you want aliases to be available when it runs shopt -s expand_aliases -- Note that a function is better than an alias since an alias is essentially just a nickname for a command, while a function permits some logic and can also be used in scripts. Here's an example of a (very) simple function: function e { echo $1 echo $1 $1 echo $1 $1 $1 } whose definition you can put in .bashrc and then you can invoke it like so: e hello -- How to "kill" another of your own logins if you have too many First issue the following command and note the value in the TTY column (it might be something like pts/14, for example): ps Then give the following command using that value: pkill -9 -t pts/14 --- wget commands to "grab" files from public websites wget http://ps.cs.smu.ca/~u30/examples_mysql.zip [to get a single zip file] wget -O t.zip http://ps.cs.smu.ca/~u30/examples_mysql.zip [and rename it] wget -m https://example.com [mirror a website] wget -m -k -p https://example.com [k=convert links to local; p=get all necessary files] wget -i url_list.txt [do several downloads from URLs listed in the file] Website to generate control sequences for echo or printf colored output https://ansi.gabebanks.net ============================================ Special Features Available on csci.cs.smu.ca ============================================ -- Command to display directory contents in a convenient form ll -- Command to format a Java source code file and keep the same name, while putting the original into another file with the same name and an additional .orig extension fjc filename.java -- Command to compile and run a program consisting of a single-file Java program in the current directory jcr filename.java -- Command to compile and run a program consisting of a Java program in the current directory that also uses the instructor-supplied utility classes jcru filename.java -- Command to get a report on any TAB characters in your Java source file find_tabs filename.java Note: Always use this command and correct TAB problems before using find_long_lines. -- Command to get a report on any too-long lines in your Java source file (all lines should be strictly less than 80 characters in length) find_long_lines filename.java Note: Always use find_tabs and correct TAB problems before using this command. -- An abbreviation for the public directory in the u00 account $pub00 -- Abbreviations for the directories where the instructor-supplied files for each non-zyBook submission (to your uxx account) are located: are located $sup01, $sup02, $sup03, $sup04, $sup05, and so on ... (if necessary) -- Commands to delete all .class and/or .orig files in the current directory rmc (removes all .class files) rmo (removes all .orig files) rmco (removes all .class files and all .orig files) ============================================ -- To make some aliases temporarily available for the current sessions source aliases.shop -- To start/stop a script displaying lines as they are executed set -o xtrace set +o xtrace ============================================