class: center, middle, inverse, title-slide # The file system and basic unix shell ## Introduction to Data Science (BIOL7800)
https://introdatasci.dlilab.com/
### Daijiang Li ### LSU ### 2021/09/21 --- class: middle ### A file system is a logical collection of files on a partition or disk #### Unix filesystem (common folders): | Directory | Description | |:----------------:|:------------------------------------------------------------------------:| | `/` | Root directory | | `/bin` | Executable files for all users | | `/dev` | Device drivers | | `/home` | Home directory for users | | `/tmp` | Temporary files used between system boots | | `/usr` | Used for miscellaneous purposes, e.g. admin commands, shared files, etc. | | `/Users` (macOS) | Files of different users | ??? a disk can have multiple partitions --- # File tree ```bash # Debian / mint / Ubuntu sudo apt-get install tree # macOS & linux (with homebrew installed) brew install tree ``` .pull-left[ ```bash tree -L 3 . ``` ``` . ├── libs │ ├── header-attrs │ │ └── header-attrs.js │ └── remark-css │ ├── default-fonts.css │ └── default.css ├── presentation.Rmd └── presentation.html 3 directories, 5 files ``` ] .pull-right[ ```r fs::dir_tree(path = ".") #R ``` ``` ## . ## ├── libs ## │ ├── header-attrs ## │ │ └── header-attrs.js ## │ └── remark-css ## │ ├── default-fonts.css ## │ └── default.css ## ├── presentation.Rmd ## └── presentation.html ``` ] --- # Naming files and directories .font150[ - All names are case sensitive - You can use pretty much any character except `/` + **Better to avoid space in names!** (looking at you, `Box Sync`!) + Also try to avoid special characters such as `>`, `|`, `:`, `&`, `!` + `\` backslash-escaping the character (i.e. quote it). `ls ~/Box\ Sync` + files and directories start with `.` will be hidden - Names are generally limited to 255 characters - Use date format ISO: YYYYMMDD or YYYY-MM-DD ] --- # Naming files and directories .font150[ - Name should be consistent (machine friendly) and descriptive (human readable) + `data.csv` (not meaningful) + `Li_plant_diversity_WI_2020-06-08.csv` (better) - Name files so they can ordered + `00_pkg_functions.R` + `01_data.R` - Have a file extension that matches the file format ] ## .blue[Take time to practice and adopt these styles and strategies can save you a lot of time in the future] --- # Absolute vs. relative paths `pwd` to check where you are right now. Let's assume we are at `/Users/dli/IntroDataScience/lectures/06_bash` .pull-left[ ### Absolute path ```bash # list files within the current folder ls /Users/dli/IntroDataScience/lectures/06_bash # move one level up? cd /Users/dli/IntroDataScience/lectures # move two levels up? cd /Users/dli/IntroDataScience # move to the home folder? cd /Users/dli # /home/user ``` ] .pull-right[ ### .blue[Relative path] ```bash # list files within the current folder ls . # move one level up? cd .. # move two levels up? cd ../.. # move to the home folder? cd ~ ``` ] --- # Some basic commands .font150[ - `man` to read the manual! Press `q` to quit, `space` to read next page (`--help` of no `man`) - `Tab` for auto-completence - Creating, removing, changing directories + `mkdir` + `rmdir` + `cd` - Moving, coping, removing files + `touch` to create empty files + `mv` + `cp` + `rm`: notable options: `-r`, `-f` (dangerous `rm -rf`) ] --- class: center, middle, inverse .font200[.red[Warning:] no undo button on the command line, so be extremely careful with deleting files/directories! I recommend to use `mv` to move files to the Trash Bin instead. (`~/.Trash` on Mac, `~/.local/share/Trash` on Ubuntu)] --- # Some basic commands .font150[ - List files with `ls`: `-a`, `-l` - Wildcard to work with multiple files and folders + `ls /bin/*sh` + `ls /bin/*[ab]*` + `mv /path/to/*.jpeg /new/path` - Check history of used command `history` + `!200` to rerun the 200th command from history - `open` (macOS) or `start` ] --- # The Unix Philosophy ### Each program should **do one thing well** ### Programs can be linked with the pipe operator `|` to do more complex tasks ```bash ls -l | head -n 3 head -100 file.csv | tail -20 | > file_81_100.csv ``` --- ## Moving cursor inside bash .font200[ + `^` means `Ctrl` key, e.g. `^A` means `Ctrl+A`. + `^A` move to the beginning of a line in the shell. + `^E` move to the end of a line. + `^C` cancel what you are doing. If it does not work, try `^\`. + `^D` end of a line. This normally will kill the job too. + `^L` clean the screen of your shell. + `^h` delete back one character (backspace). + `^w` delete back one word. + `^u` delete back to the start of line. ] --- class: middle, inverse ## More common commands see [course webpage](https://introdatasci.dlilab.com/2021-08-18-shell-commands/)