Working with Files and Directories

Creating Files and Directories: touch, mkdir
Making Directories (mkdir)
create folder to practice in
from home use the command
mkdir Linux_Practice
move 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) flag
command creates parent folder(s)
mkdir -p Project/Images/HighRes
note relative path

Creating Files (touch)
fastest way to create a blank, empty file is to use the touch command
touch document.txt
command 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

  • report_january.txt
  • report_february.txt
  • photo.jpg
  • audio.mp3

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 Linux
treated 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 overwritten
to prevent this use the -i flag
mv -i report_february.txt final_report.txt
system 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 TempFolder
as a safety feature rmdironly removes empty directories
mkdir FullFolder
touch FullFolder/file.txt
rmdir FullFolder
output
rmdir: failed to remove 'FullFolder': Directory not empty
Removing Files (rm)
to delete a file use the command
rm report_january_backup.txt
no 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 FullFolder
if a file is write-protected the command will prompt for confirmation
to bypass all checks and confirmations use the force -f flag
rm -rf FullFolder
quietly 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: empty
running 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' shortcut
a 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_report
listing the folder contents with ls -l
lrwxrwxrwx 1 n4jvp n4jvp   16 Jan  1 18:01 link_to_report.txt -> final_report.txt
the 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_file
the ls command will not show the file
the ls -a command will show the file

Why Use Hidden Files?
used mostly for configuration files
files are hidden to view clean
Finding Files: find, locate, which, whereis
Quick Search: locate
locate is the fastest way to locate a file
does not search hard drives
searches a database (index) of all files on the system
to locate every file with a .jpg extension
locate .jpg
the database is updated once a day
newly created files may not appear in the list
an update can be forced using the command
sudo updatedb
takes time to update database

Note : here the system responded with

Command 'locate' not found, but can be installed with:
sudo apt install plocate
during 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 time
the syntax
find <where to look> <criteria> <what to search for> 
to find a specific file
find . audio.mp3
the dot represents the current folder
search is case-sensitive

for a case-insensitive search use the -iname flag

find . -iname audio.mp3
to find all files in home directory (~) which are larger than 10 Megabytes
find ~ -size +10M
to find only directories with the same name
find . -type d -name "<directory name>"
to find all directories from home
find ~ -type d
to 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 this
quote 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 ls
output
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 characters
case-sensitive
  • list : file[123].txt matches file1.txt, file2.txt, file3.txt
  • range : Image_[A-Z].jpg matches Image_A.jpg, Image_B.jpg ...
Wildcard Warning
always check the command line multiple times when using rm command
especially neded when using wildcards

Best Practices for File Organization
Home Directory is Sacred
do not anything direcly in the home directory
awlays put files in subdirectories

No Spaces in Filenames
use an underscore or hyphen instead of spaces
Use ISO Dates
use YYYY-MM-DD format
ISO dates sorts properly and consistantly

Logically Hierarchy
organize files using subfolders
manage 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

index