Basic Operations include:
- Creation
- Removal
- Move and Rename
- View contents
- Search
Creating files and directories
- For creating files,
touchis used. This tool is also used to change the timestamp of the file (or reset it). For example with the-tflagtouch -t 12092018 myfilesets the date ofmyfileto the 9/12/2018 - Directories are created with
mkdir
Creating files with content
For this, there is two options from the command line:
- Use a text editor
- Use a command:
echotogether with write and append redirectionecho line one > file1 echo line two >> file2cattogether with redirectioncat << EOF > file1 line one line two EOF
Removing
- Removing a directory is done with
rmdir. The directory must be empty - To remove a directory and all of its contents
rm -rf. rmRemoves a file (Also accepts patterns). Has additional options, for example–fto forcefully remove a file or–ito interactively remove it(prompt every time)
Moving and renaming
For files and directories both can be done with the mv command.
mv file other_filechanges the file namemv file dir/dir/moves the filemv file dir/dir/other_filemoves the file and changes the name
Viewing files
catPrints file content to screentacSame ascatbut starts from the bottom uplessUsed to view larger files because it is a paging program (Add scrolling). Special options can be used to search for patterns (/for forward and?backwards)tailPrint the last 10 lines of a file (with the -n flag or -15 the number can be changed)headPrint the first 10 lines of a file (with the -n flag or -15 the number can be changed)
Search files
Wildcards
Basic search commands (and ls, among others) allow for the use of wildcards to search for files:
?Matches any character*Matches any string (E.g*.docwould match all .doc files)[abc]Matches any character inside the brackets (-can be used for ranges)[!abcMAtches any character except the ones in brackets
locate
- Performs a search taking advantage of a previously constructed database of files and directories on your system.
- Matches all entries that contain a specified character string.
- The database is constructed with
updatedb. (Automatic in most linux distros once a day, can be run manually). - Can be piped with
grepto further filter the results - Does not come in Ubuntu 19+ by default
find
- Recurses down the filesystem tree from any particular directory (or set of directories) and locates files that match specified conditions.
- The default pathname is always the present working directory.
- Different flags can be used (See
man find):find . -name wa #Finds files and directories named wa find . -iname wa #Ignore case in the name matching find . -type d -name wa #Only directories find . -type f -name wa #Only files - Also allows to directly process the results using the
-execoption. Uses{}as placeholder for the results. (-okcan also be used to prompt every operation)find -name "*.swp" -exec rm {} ';' #Finds and removes all .swp files - Other tests to match include
ctime nWhen the inode metadata last changed (When it was created) (nis the number of days, can also be+nfor bigger than and-n)atime nAccessed/Last Read timemtime nModified/Last Written timecminaminmminSame but in minutessize nUSize of the file,nis the size (can also be+no-n) andUis the unit (by default 512 bytes blocks,kfor kilobytes,Mfor megabytes,Gfor gigabytes).