Linux stuff: Difference between revisions
Line 54: | Line 54: | ||
*cp -i *.txt /destination/: Copy all .txt files, prompting before overwrite | *cp -i *.txt /destination/: Copy all .txt files, prompting before overwrite | ||
*cp -p source dest: Preserve file attributes (mode, ownership, timestamps) | *cp -p source dest: Preserve file attributes (mode, ownership, timestamps) | ||
== mv - Move or rename files and directories == | |||
mv can move files/directories or rename them. | |||
Examples: | |||
*mv file.txt newname.txt: Rename a file | |||
*mv /home/user/doc.txt /tmp/: Move a file to another directory | |||
*mv dir1 dir2: Rename a directory (if dir2 doesn't exist) or move dir1 into dir2 | |||
*mv -i *.jpg /home/user/Pictures/: Move all .jpg files, prompting before overwrite |
Revision as of 14:43, 19 February 2025
Common Commands with examples
These commands and their usage are used for both the LinuC Level 1 Exam 101 and Exam 102. Using these commands in various scenarios to become proficient in basic command-line tasks, file management, and system administration.
ls - List directory contents
The ls command is used to list files and directories in a specified location. It has many options to customize the output.
Examples:
- ls -l: Long listing format, showing permissions, owner, size, and modification date
- ls -a: Show all files, including hidden ones (those starting with a dot)
- ls -R: Recursively list subdirectories
- ls -lh: Use human-readable file sizes (e.g., 1K, 234M, 2G)
- ls /etc: List contents of the /etc directory
cd - Change directory
cd is used to navigate the file system. It can use absolute or relative paths.
Examples:
- cd /home/user: Change to the /home/user directory (absolute path)
- cd ..: Move up one directory level
- cd -: Return to the previous directory
- cd ~: Change to the user's home directory
- cd Documents/Projects: Change to a subdirectory (relative path)
pwd - Print working directory
pwd displays the full path of the current working directory.
Examples:
- pwd: Outputs something like /home/user/Documents
- pwd -P: Print the physical directory, without any symbolic links
mkdir - Create a new directory
mkdir is used to create one or more directories.
Examples:
- mkdir newdir: Create a directory named "newdir"
- mkdir -p parent/child/grandchild: Create nested directories
- mkdir dir1 dir2 dir3: Create multiple directories at once
- mkdir -m 755 secured_dir: Create a directory with specific permissions
touch - Create an empty file or update file timestamps
touch can create empty files or update the access and modification times of existing files.
Examples:
- touch newfile.txt: Create a new empty file or update timestamps if it exists
- touch -t 202502191200 file.txt: Set specific timestamp (YYYYMMDDhhmm)
- touch -r reference_file target_file: Use timestamps from reference_file
cp - Copy files and directories
cp is used to copy files and directories from one location to another.
Examples:
- cp file.txt /home/user/backup/: Copy a file to another directory
- cp -r dir1 dir2: Copy a directory and its contents recursively
- cp -i *.txt /destination/: Copy all .txt files, prompting before overwrite
- cp -p source dest: Preserve file attributes (mode, ownership, timestamps)
mv - Move or rename files and directories
mv can move files/directories or rename them.
Examples:
- mv file.txt newname.txt: Rename a file
- mv /home/user/doc.txt /tmp/: Move a file to another directory
- mv dir1 dir2: Rename a directory (if dir2 doesn't exist) or move dir1 into dir2
- mv -i *.jpg /home/user/Pictures/: Move all .jpg files, prompting before overwrite