Tech

Notes: List of Terminal Commands

List of Terminal Commands:

Note: These were for Windows but some commands work on Mac/Linux).

pwd print working directory

hostname my comuter’s network name

mkdir make a new directory

cd change directory forward

cd.. change to previous directory (or ‘cd../../..‘ to go back 3 times, etc.)

ls list storage (list directory)

ls -l -long listing (gives more info with the ‘-l’ flag option)

rm remove a file

rmdir remove directory

rm -rf<dir> to force remove, if needed.

pushd push directory

popd pop directory

cp copy a file or directory

robocopy robust copy

mv move (rename) a file or directory

more (or cat) page through a file

type print the whole file (print output to screen)

forfiles run a command on lots of files

dir -r find files

select -string find things inside files

help read a manual page

helpctr find what man page is appropriate

echo print some arguments

touch create a simple file

cd~ takes you to your home directory

set export/set a new environment variable

exit exit the shell

If lost in Powershell, type ‘pwd’ to see where I am, then ‘cd~’ to get home to the Home directory.

vim opens Vim text editor

nano opens Nano text editor

CLI (Command Line Interface)

GUI (Graphical User Interface)

More CLI Terminal Commands

curl wttr.in outputs the local weather report!

ls -a outputs both “visible” and “hidden” files and directories. (“Hidden” files are usually prefixed with a ‘.’ dot.)

ls -l long-form listing. Lists files & directories with more detailed info.

-h this flag option puts bytes in human readable sizes. ex: ls -lh

-t sorts by last modified time. ex: ls -lt

Note: These ‘-‘ (dash) flags can be combined and used in conjunction with each other like: $ ls -laht (long listing, all files “visible” & “hidden”, human readable byte sizes and time modified!)

cal outputs calendar month

cal -y outputs current calendar year

cal 2015 outputs another year; 2015 in this case.

Opening folders, files, & applications from the CLI!

The ‘open‘ commands opens a file, directory (folder), application or a URL, just as if you had double-clicked the files icon.

If no application name is specified, the default application is used to open the file, as determined by Launch Services. (You can also make use of this to open a file in a different application than its default app by specifying one.)

To open a directory in the Finder, use “open” followed by the name of the directory.

open. –Note the ‘dot’ at the end! This opens a ‘Finder’ window containing the current directory.

open ~/Public –opens your Public folder (~/Public) in a ‘Finder’ window.

open /Applications –opens the ‘/Applications’ folder in a ‘Finder’ window.

open -a Calculator.app –opens the Calculator app.

Ctrl+C –“get out of trouble” command; use if the Terminal ‘hangs’ or stalls.

man <command> –Display manual page for given command.

man sleep –Displays manual for the “sleep” command. Note: Hit ‘q’ to exit the manual page.

Navigating on the prompt line:

Ctrl+A –moves cursor to beginning of the line.

Ctrl+E –moves cursor to the end of the line.

Ctrl+U –deletes the line

Option + mouse click -moves cursor to desired location on the line; helpful for long text lines.

(Arrow up) or (Arrow down) –press ‘arrow-up’ or ‘arrow-down’ to scroll through previous commands.

clear or Ctrl+L –clears the Terminal screen

exit or Ctrl+D –exits the Terminal.

Redirecting & Appending

We can use the CLI to create files with text already included without having to use a Text Editor.

Ex:

  • echo “From fairest creatures we desire increase.” > sonnet_1.txt
    • This will create a new .txt file named sonnet_1.txt in the current directory.
    • Note: Notice the use of the right angle bracket (‘>’), as the ‘redirect operator’.
  • If we use ‘redirect operator’ again with different text it will write over the file.
  • BUT, we can use the ‘append operator’ (‘>>’), to add more text.
  • Note: Notice the ‘append operator’ is 2 right brackets. This just adds, or appends, the line to the end of the given file.

More CLI Terminal Commands

diff –compares files passed as arguments for differences between them.

  • ex: diff sonnet_1.txt sonnet_1_lower_case.txt
    • Note: This will only print an output if the files are, in fact, different. If nothing is printed, then they are the same.
  • *–wildcard character
    • ex: ls *.txt –this will list al the files ending in ‘.txt’.
    • or ls s* –this will list all the files that start with “s”, in that directory.
    • or ls *onnet* –use a wildcard character operator at the beginning and the end to narrow down a string of characters in the middle. Like “onnet” from “sonnet”!
    • Note: The wildcard operator should go at the end of the command after the other options. ex: ls -a -lr *onnet*

ls -r –list by reverse

ls -t –list by time of modification

ls -l –list by long format

Note: We can combine these together like:

ls -rtl –list by reversed time of modification (long format). Note: This can be helpful when there are a lot of files in a directory but we really only care about seeing the ones modified recently, such as when confirming a file download!

Note: The “ls” command simply lists all the files and directories (folders) in the current directory. It’s effectively a command-line version of the graphical user browser (or the ‘Finder’ window).

*It should be noted that the “ls” command can be used to check it a file (or directory) exists, because trying to “ls” a nonexistent file will result in an error message!

Also, remember that we can “ls” a sub-directory of the current working directory by passing the name of the sub-directory as an argument to “ls” and we’ll get to see that info without having to leave the current working directory.

Renaming, Copying, Deleting

  • Renaming
  • mv –The way to rename a file is with the “mv” command, short for “move“.
    • Ex: echo “test text” > test
    • mv test test_file.txt #’test’ is the old-name, and ‘test_file’ is the new-name.
    • ls
    • test_file.txt
    • #The above renames the file called “test” to “test_file.txt”.
    • Note that while the command is called “mv” or “move, it is actually renaming!
  • Copying
  • cp –The way to copy a file is with “cp”, short for “copy”.
    • Ex: cp test_file.txt second_test.txt #’test_file.txt‘ is the original-file, and ‘second_test.txt‘ is the new file.
    • ls
    • second_test.txt
    • test_file.txt
    • #This copies the file “test_file.txt” to another file we are naming “second_test.txt“.
    • Note that since this copies, both files remain.
    • *NOTE: cp -r <old directory> <new directory> #this will copy the contents of the original directory to the new one!
  • Deleting
  • rm –The command for deleting a file is “rm”, for “remove”.
    • Ex: rm second_test.txt
    • ls second_test.txt
    • ls: second_test.txt: No such file or directory