File and directory command line

File and directory command line

Directories

List directory: ls
List directory (wildcard matching): ls *.txt
List all files of type: find . -name “*.txt” -print
Switch/toggle between dirs: cd –
Make empty directory: mkdir sample-directory
Remove empty directory: rmdir sample-directory
Remove directory with all contents: rm -rf sample-directory/
Remove directory contents and keep directory: rm -rf *
Checkout directory: cd sample-directory
Browsing directories: pushd sample-directory / popd / dirs

 

Files

Make (empty) file: touch sample-file.txt
Change creation date: touch –t 201501011337 sample-file.txt
Change modified date: touch –mt 201501011337 sample-file.txt
Duplicate a file: cp sample-file.txt sample-file-copy.txt
Move/Rename a file: mv current-filename.txt new-filename.txt
Remove a file: rm sample-file.txt
View a file: less sample-file.txt / more sample-file.txt
Write to a file (overwrites existing content): cat > sample-file.txt (quit with ctrl+d)
Search for a filename in the current directory: find sample-file.txt
Search for a string inside all files in the current directory: ack “string”
Search for a string inside all files in the current directory and subdrectories: grep -r “string” *
Search and replace within file: sed -i ” ‘s/original-text/new-text/g’ sample-file.txt

SHARE
Previous articleConnect a capacitive touch sensor
Next articleRaspberry PI GPIO connector