This content originally appeared on DEV Community and was authored by Aravinthan
Linux – File Commands
This is Part 2 of the Linux CLI Commands series.
Previous: Linux – System Commands
Next: Linux – Process Commands
File Commands
search files only in current directory
find . -maxdepth 1 -type f
ensures it won’t go into subdirectories (-maxdepth 1)
current directory (.)
only files (-type f)
ls -p | grep -v /
ls -p
appends/
to directory names
grep -v /
excludes anything ending in /
, leaving only files
ll
— long list file and directory
ls
— short list file and directory
ll -lrt
— file and directory are order by time
touch
— use to create a file
cat filename
— to read a files
cat > filename
— to create a file and also insert data
cat >> filename
— to append dates
mkdir
— use to create a directory
vi filename
— use to edit a file
yy
— to copy a single line
dd
— delete a single line
rmdir
— to remove a dir
rmdir -rf
— delete directory and inner files rf means recursive force
rm
— to delete a file
rm -rf
— if rm isn’t work use rm -rf, rf means recursive fource
head filename
— show first 10 line
tail filename
— show last 10 line
Show line numbers (-n)
grep -n "failed" logfile.txt
grep -v "DEBUG" logfile.txt
Match whole words only (-w)
grep -w "root" /etc/passwd
nl filename
— show date with number line and skip the space in between lines
cat -n filname
— show all line with number
cp
— copy file
cp -R
— to copy a directory
diff file1 file2
— to compare to files, output is only the different data
comm file1 file2
— output with three column, fist col is file1 different data and second col is file2 different data and third col is common data col
wc filename
— list out the number of line, words, bites
pwd
— present working directory
cd
— change directory
cd ..
— previous directory
mv
— move or rename a file or directory
sort filename
— use sort a file in ascending order
sort -n filename
— use to sort numerical data
uniq filename
— remove duplicates
sort file.txt | uniq > output.txt
sort file.txt | uniq > temp.txt && mv temp.txt file.txt
split -3 filname
— splite a file into three
paste filename1 filename2 > filename3
— file1 and file2 past or merge in file3
fmt filename
— convert column into a single row
strings filename
— use to read a file understandable format
This content originally appeared on DEV Community and was authored by Aravinthan