[cb]: https://collectionbuilder.github.io/ "CollectionBuilder" ## MacOS/Linux Command-Line Basics --- ## mac/unix/linux directories - directory = folder - home directory: your personal user directory - `/Users/jawalsh` - `/Users/guest` - directory structure is a tree structure - root directory : `/` --- ![directory tree structure](images/cli/linux-directory-structure.webp) image credit:
--- ## `ls`: view the files in a directory/folder - `ls` : list files - `ls -l` : list files in long format, with more information - `ls -lh` : list files with sizes in bytes, kilobytes, megabytes, gigabytes, etc. --- ## `cd`: change directories - `cd <directory name>` - `cd /Users/jawalsh/Documents` --- ## Directory shortcuts - `~` = home directory - `cd ~` - `..` = parent directory (go up one level in the tree) - If I am in the directory `/Users/jawalsh/Documents/french/f361` and I run the command `cd ..`, I will change directories to `/Users/jawalsh/Documents/french/` - If I am in the directory `/Users/jawalsh/Documents/french/f361` and I run the command `cd ../f250`, I will change directories to `/Users/jawalsh/Documents/french/f250` --- ## `mkdir`: Make directory - `mkdir z652` - `mkdir /Users/jawalsh/Documents/z652` --- ## `pwd`: print working directory (a.k.a. “Where am I?”) - `pwd` will print the full file path to your current location in the directory/folder tree. ![pwd command](images/cli/pwd.png) --- ## `rm`: unlink - remove directory entries (a.k.a. “delete”) - `rm
` ![rm command](images/cli/rm.png) --- ## `cp`: copy - `cp myfile.txt myfile_backup.txt` ![cp command](images/cli/cp.png) --- ## `mv`: move or rename files - `mv mydemo.txt mydemo_backup.txt` ![!mv command](images/cli/mv.png) --- ## `mv`: move or rename files - `mv ~/Downloads/bookends.dmg ~/Desktop` ![!mv command](images/cli/mvdir.png) --- ## `$PATH` environment variable The `$PATH` environment variable tells the operating system where to look for executable commands (commands, programs) when you type them on the command line. All the commands we’ve just reviewed should be in directories that are already in your `$PATH`. If you install software using homebrew, those commands should automatically be added to directories in your `$PATH`. You can see your current path with the command: - `echo $PATH` If you need to add a directory to you `$PATH`, consult
--- ## Commands and file paths When you are referring to files on the command line, you need to be in the same directory as the files you are referring to **or** give the full path to the file. For instance, consider these two directories: - `/Users/jawalsh/my-web-site` contains the HTML and other files for my personal web site. - `/Users/jawalsh/my-web-site/images` contains the images for my web site. --- ## Commands and file paths If I am in the directory `/Users/jawalsh/my-web-site` and I type the command `magick image.jpg -scale 50% image_sm.jpg` to create a smaller version of an image file, the command will not work if my image files are in `/Users/jawalsh/my-web-site/images`. So I can change directories to `/Users/jawalsh/my-web-site/images` and run the command, and it will work. --- ## Commands and file paths Or… I can be in any directory and tell the command the full path to the image files, e.g.: - `magick /Users/jawalsh/my-web-site/images/image.jpg -scale 50% /Users/jawalsh/my-web-site/images/image_sm.jpg`