Linux: File/Directory Operations
By Xah Lee. Date: .
List Files
ls
-
List files in current directory
ls -al
-
List all files in current dir, including dot files, and
also show detail, such as File Permission
, owner, and size.
ls -al | grep string
-
Show file name matching string
Create a new file
touch fileName
-
Create a new file with empty content.
If the file exists already, update its timestamp.
Create a new file link
ln -s newPath existingPath
-
Create a new file that is a symbolic link (aka soft link) to another file.
(symbolic link is a file whose content is a file path to another file.)
ln newPath existingPath
-
Create a new file that is a hard link of a file.
(Hard link makes 2 files pointing to the same index in the file system (hard disk).)
Delete File/Dir
rm fileName
-
Delete a file
rm -r dirName
-
Delete a directory. (BE CAREFUL)
rmdir dirName
-
Delete a dir only if it is empty
Create a new dir
mkdir newDirName
-
Create a new dir
Copy a file/dir
cp fileName newFileName
-
Copy a file
cp -r dirName newName
-
Copy a dir
Move/Rename File/Dir
mv fileName newName
-
Rename file, or move to a diff dir.
Show Dir Size