| Creating Files and Directories: touch, mkdir |
|
Making Directories (mkdir)
create folder to practice infrom home use the command mkdir Linux_Practicemove to the folder cd Linux_Practice Creating Nested Directories
create a full folder structure
Project
Images
Docs
Source
instead of creating each fold individually use the -p (parent) flagcommand creates parent folder(s) mkdir -p Project/Images/HighResnote relative path Creating Files (touch)
fastest way to create a blank, empty file is to use the touch command
touch document.txtcommand created to update file's timestamp when an existing file is touched, its timestamp is updated if file doesn't exist it will be created use touch to create the following files to work with
create a folder as well mkdir Archives |
| Copying, Moving, and Renaming: cp, mv |
|
Copying Files (cp)
cp command creates a duplicate file
cp <source> <destination>copy a file cp report_january.txt report_january_backup.txt Copying to a Directory
forward slash at end indicates a directory
cp photo.jpg Archives/ The Recursive Copy
to copy a folder use the -r (recursive) flag
cp -r Project Archvies/ Moving Files (mv)
to move a file use the mv command
mv audio.mp3 Archives/ Renaming Files (The Secret of mv)
no rename command in Linuxtreated as move moving file from one name to another mv report_february.txt final_report.txt Safety Warning : Silent Overwrites
when copying or moving a file, if the file already exists it will be overwrittento prevent this use the -i flag mv -i report_february.txt final_report.txtsystem will prompt cp: overwrite 'final_report.txt'? |
| Deleting Files and Directories: rm, rmdir |
|
Removing Empty Directories (rmdir)
create temporary folder before deleting it
mkdir TempFolder rmdir TempFolderas a safety feature rmdironly removes empty directories mkdir FullFolder touch FullFolder/file.txt rmdir FullFolderoutput rmdir: failed to remove 'FullFolder': Directory not empty Removing Files (rm)
to delete a file use the command
rm report_january_backup.txtno confirmation or response the file is just gone The Nuclear Option: rm -tf
to delete a populated folder use the recursive -r command
rm -r FullFolderif a file is write-protected the command will prompt for confirmation to bypass all checks and confirmations use the force -f flag rm -rf FullFolderquietly deletes everything |
| Understanding File Types: Regular Files, Directories, Links, and Special Files |
|
Linux ignores file extensions can name a text file 'textfile.jpg' and it will be treated as a text file can be opened in a text editor can use the file command to determine what the file is file textfile.jpg textfile.jpg: emptyrunning the command against a real image image.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), ... Links: Hard vs Soft
a soft link aka symbolic link/symlink is like a Windows' shortcuta special file which points to another file if original file is deleted, the link is broken the command structure to creat a symlink is ln -s <target file> <link name>create a symlink ln -s final_report.txt link_to_reportlisting the folder contents with ls -l lrwxrwxrwx 1 n4jvp n4jvp 16 Jan 1 18:01 link_to_report.txt -> final_report.txtthe permission string starts with a l indicating the file is a link if link_to_report.txt is edited, final_report.txt is actually being edited a hard link is not a shortcut a hard link is like a named pointer to a file if a hard link to textfile.txt is tf.txt, both names point to the same file if textfile.txt is deleted the data is still there and can be accessed via tf.txt data is only deleted when all hard links are deleted hard links are rarely used |
| Working with Hidden Files and Directories |
to hide a file simply start the filename wiith a dot (.)
touch .secret_filethe ls command will not show the file the ls -a command will show the file Why Use Hidden Files?
used mostly for configuration filesfiles are hidden to view clean |
| Finding Files: find, locate, which, whereis |
|
Quick Search: locate
locate is the fastest way to locate a filedoes not search hard drives searches a database (index) of all files on the system to locate every file with a .jpg extension locate .jpgthe database is updated once a day newly created files may not appear in the list an update can be forced using the command sudo updatedbtakes time to update database Note : here the system responded with Command 'locate' not found, but can be installed with: sudo apt install plocateduring the install it was determined the llvmll19 package is no longer required the package can be removed by using the command sudo apt autoremove Deep Search: find
find searches the hard drive in real timethe syntax find <where to look> <criteria> <what to search for>to find a specific file find . audio.mp3the dot represents the current folder search is case-sensitive for a case-insensitive search use the -iname flag find . -iname audio.mp3to find all files in home directory (~) which are larger than 10 Megabytes find ~ -size +10Mto find only directories with the same name find . -type d -name "<directory name>"to find all directories from home find ~ -type dto find all files with names ending in .tmp and delete them this is dangerous find . -name "*.tmp" -delete Finding Programs: which
need to consider hows/whys of thisquote from the text Sometimes you type a command like python, and you want to know exactly which file is being executed.
Type:
which python3
Output:
/usr/bin/python3
This tells you the absolute path of the executable.
If which returns nothing, the program is not installed or not in your PATH.
Manual Finder: whereis
command returns where the binary is, where the source code is, and where the manual page is
whereis lsoutput ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz |
| File Globbing and Wildcards: *, ?, [] |
|
selecting multiple files as a group use wildcards aka Globbering Asterisk (*)
matches zero or more characters
Question Mark (?)
matches exactly one character
Square Brackets ([])
specify a range or list of characterscase-sensitive
Wildcard Warning
always check the command line multiple times when using rm commandespecially neded when using wildcards |
| Best Practices for File Organization |
|
Home Directory is Sacred
do not anything direcly in the home directoryawlays put files in subdirectories No Spaces in Filenames
use an underscore or hyphen instead of spacesUse ISO Dates
use YYYY-MM-DD formatISO dates sorts properly and consistantly Logically Hierarchy
organize files using subfoldersmanage number of files in a folder |
| Summary |
|
mkdir -p : create nested directories touch : update a timestamp or create an empty file cp -r : recursive copy, for copying directories mv : move or rename files rm : deletes diles permanently rm -rf : forces recursive delete of everything, dangerous file extensions : only have meaning to users hidden files : any fiename start with the dot operator is hidden find : search tool for files and directories wildcards : as named |