File and directory management

From English Wiki
Revision as of 04:11, 15 August 2022 by Kentlee7 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The following are common or important commands for finding and managing files and directories in Linux.


1 File and directory management

Command Function
cat file Display contents of file on screen
cat file1 file2 > file3 Concatenate file_1 and file_2 and sends combined contents to new file_3
cat file1 >> file2 Append the content of file1 to the end of file2
cat > file Create new empty file
cd Move to home directory
cd - Move back to previous directory
cd .. Move up to parent directory
cd “dir Navigate to the specified directory
cd / Move to root directory
cd or cd ~ Move to the home ($HOME) directory
cp -a -R -i Copy directories or files from an active directory instance.
cp -r dir1 dir2 Recursively copy the contents of directory1 to directory2
cp -r source_dir destination Copy the source_directory recursively to the destination (if the destination exists, the source_directory is copied into the destination, or otherwise the destination is created with the contents of source_directory .
cp -r /source_dir*/*.* /dest_dir Recursively copy a number of files from multiple subdirectories to a new directory)
cp -t /dest_dir */*.png Recursively copy a number of files from multiple subdirectories (of a certain type) to a new directory)
cp file1 file2 Create a copy of file1, named file2
file -L file Follow a symbolic link for a file to determine its type
file file Determine what type of file the file name is; file -z for compressed files
xargs mv –target-directory=’’dir’’ Search through a large directory to move all files (of a certain type) to a new directory (useful when a regular mv, cp or other command yields error messages like “argument list too long” for an extremely large number of files or subdirectories)
gpg -c file Encrypt a specified file.
gpg file.gpg Decrypt a specified file with a .gpg extension.
head file Display the first ten lines of a file
less file Browse through the contents of a text file; like more, but with the ability to page back and forth
ln -s path-to-file Create a symbolic link to a specified file
ls List directories and files in current directory
ls -a Lists all files including hidden files
ls -al List all files and directories with full information, e.g., size, permission, and owner.
ls -hl Display file sizes in human readable format (e.g.,. MB, GB)
ls -l List all files in detailed format
ls -l file Show file information of a specified file
ls -R Displays files in current directory and all subdirectories
ls -rt List files in reverse chronological order (according to when they were modified)
ls -t List and sort files chronologically (according to when modified)
mkdir “dir' # Create a directory
more file Open and display contents of a file
mv Move or rename a file
mv -f Forcibly move or rename a file
mv -t /dest_dir */*.png Recursively move a number of files from multiple subdirectories (of a certain type) to a new directory)
mv file newname Renames file to new name
mv file "dir" Moves file to specified path (directory)
mv file” newname Rename a file
mv file1 file2 # Rename or move file1 to file2 . If file2 is an existing directory, move file1 into directory file2
pwd Display the current directory
rm -f file Forcefully delete a file without prompting for confirmation
rm -r dir Remove the directory and its contents recursively
rm -r “dir' Delete a directory
rm -rf “dir' Recursively and forcefully delete a directory
.bar|*.baz) Delete all files in a directory that do not match a certain file extension
rm file Delete a file
rm file Delete file
rmdir dir Delete directory
rmdir-r dir Remove a directory and its contents recursively
stat -f device Display status of a filesystem (e.g., /dev/sda)
tail -f file Display the last ten lines of a file (and view it as contents added to the file)
tail file Display the last ten lines of file
touch file Create a new (and empty) file
wc file Outputs number of bytes, lines, and words of a specified file name.


2 File Utilities

Command Function
aspel Intera­ctive spell checker
awk -F "­," '{print $[‘’col#’’]­>}' Print only column #n of a file
cat -s Concatenate, i.e., combine files into a new file
comm Compare two sorted files line by line
comm -3 After two files are sorted, sort them line by line.
convert "*.png" output.pdf Convert multiple PNG files into one single multi-page PDF file (via ImageMagick)
convert file.pdf file-%04d.png Convert PDF file into multiple PNG files (or other file formats), with enumerated file names (via ImageMagick)
cut -c 7-10 Cut characters 7-10 from a file
cut -f 3 file Cut column 3 from a file
cut -s Remove or delete a section from a file
dd Read bytes from a location
diff Compare files
diff -q Compare files line by line.
head -n Output the first few contents of a specified file
join -i For two files with a common field, join the lines of these files
less View contents of a file one page at a time (with the ability to go forwards and backwards)
more View file contents, one page at a time
paste Merge lines of files
patch Apply a diff file to original
pr -h Add a header to a target file.
pr -n Uses line numbers to denote a target file.
pr -x Divide or split a file to x specified columns.
sed Stream editor for modifying strings in files
sed ‘/abc/xyz/’ file Replace all all instances of a string (abc) with another (xyz) in a file
sort -n Sort the lines within a text file.
split -l Split a file into separate files
tail -1 Display the last line of a file
tail -f Output the last portion of a file.
tr Truncate a file
tr -d Truncate or deleting a character.
uniq -c Count unique lines in a file
uniq -c -u Report or omit repeated lines.
uniq -u Omit repeated lines in a file
wc -l Print number of lines in a file
wc -w Print the number of bytes, words, and newlines on a specified file.


3 File search

Command Function
find path -name pattern Trace a specific system directory location then queries the names of its files and folders based on a character match
find path -size +100M Find files larger than 100MB in /home
find /home/user -name 'prefix*' Traces a specified system path for files with a matching prefix
pattern2’ dir’ Search recursively through a directory for two patterns or strings
grep -i Case insensitive search
pattern2’ dir’ Search recursively through a directory for two patterns or strings
grep -r pattern dir Search recursively for a pattern in a directory
grep -v pattern Inverted search: exclude files with specified string
grep pattern file Search for a pattern in a file
grep, egrep, fgrep -i -v Find keywords in files with a matching pattern.
locate -i Uses updateddb to track and find files on your system.
locate [name] Find files and directories by name (searches the system’s indexed file database)
ls -a -C -h Lists or displays the content of an active directory.
updatedb Update the filesystem database used by the locate command


4 Grep options

Command Function
c Print number of matches
h For multiple outputs suppress outputting the filename
i Ignore case
l Print the name of each file that contain matches
L Print only the names of files that do not match
n Print match line with line number
o Show only matching part of file
r Search recursively
v Invert match



5 See also