Commands for File and Directory Operations in Linux
File and directory operations in Linux mainly involve the following commands:
ls- List directory contents- Usage example:
ls -l(list file or directory details in long format)
- Usage example:
cd- Change the current directory- Usage example:
cd /home(switch to the/homedirectory)
- Usage example:
pwd- Display the path of the current directory- Usage example:
pwd(display the full path of the current working directory)
- Usage example:
mkdir- Create a new directory- Usage example:
mkdir new_dir(create a new directory namednew_dir)
- Usage example:
rmdir- Remove an empty directory- Usage example:
rmdir empty_dir(remove the empty directory namedempty_dir)
- Usage example:
rm- Remove files or directories- Usage example:
rm file.txt(remove the file namedfile.txt) - Advanced usage:
rm -r dir(recursively remove the directory nameddirand its contents)
- Usage example:
cp- Copy files or directories- Usage example:
cp source.txt dest.txt(copysource.txttodest.txt) - Advanced usage:
cp -r source_dir dest_dir(recursively copy a directory)
- Usage example:
mv- Move or rename files or directories- Usage example:
mv old_name.txt new_name.txt(renameold_name.txttonew_name.txt) - Advanced usage:
mv file.txt /home/user/(movefile.txtto the specified directory)
- Usage example:
touch- Create an empty file or modify a file’s timestamp- Usage example:
touch new_file.txt(create an empty file namednew_file.txt)
- Usage example:
chmod- Change the permissions of a file or directory- Usage example:
chmod 755 file.txt(change the permissions offile.txtso that the owner can read, write, and execute, and the group and other users can read and execute)
- Usage example:
chown- Change the owner of a file or directory- Usage example:
chown user:group file.txt(change the owner offile.txttouserand the group togroup)
- Usage example:
These commands are the foundation of file and directory management in Linux systems, and mastering them is essential for effective system administration.
Linux Process Management Commands
Process management in Linux mainly involves the following commands:
ps- Display a snapshot of current processes- Usage example:
ps aux(display information about all processes)
- Usage example:
top- Dynamically display process information- Usage example:
top(display system process information in real time)
- Usage example:
htop(requires separate installation) - An enhanced version of thetopcommand, offering a friendlier interface and more information- Usage example:
htop(display system process information in real time with a friendlier interface)
- Usage example:
kill- Send a signal to a specified process- Usage example:
kill -9 1234(send the SIGKILL signal to the process with PID 1234, forcibly terminating it)
- Usage example:
pkill- Send a signal based on process name- Usage example:
pkill nginx(send the SIGTERM signal to all processes named nginx)
- Usage example:
killall- Kill all processes based on process name- Usage example:
killall nginx(kill all processes named nginx)
- Usage example:
nohup- Run a command in the background so that it continues even if the terminal is closed- Usage example:
nohup command &(runcommandin the background and ignore the hangup signal)
- Usage example:
bg- Resume a job that was suspended in the background- Usage example: first use
Ctrl+Zto suspend a process, then use thebgcommand to continue running it in the background
- Usage example: first use
fg- Bring a background job to the foreground to continue running- Usage example:
fg(bring the most recent background job to the foreground to continue running)
- Usage example:
jobs- Display all jobs in the current session and their status- Usage example:
jobs(list all jobs in the current session)
- Usage example:
nice- Set the priority of a new process- Usage example:
nice -n 10 command(startcommandwith priority 10)
- Usage example:
renice- Modify the priority of an existing process- Usage example:
renice 10 -p 1234(set the priority of the process with PID 1234 to 10)
- Usage example:
These commands are the foundation of process management in Linux systems; with them, you can effectively monitor, control, and adjust processes on the system.
Linux System and User-Related Commands
Commands related to user and user group management in Linux systems include:
useradd- Add a new user- Usage example:
useradd username(add a new user)
- Usage example:
userdel- Delete a user- Usage example:
userdel username(delete a user)
- Usage example:
usermod- Modify a user account- Usage example:
usermod -l newname oldname(change the username from oldname to newname)
- Usage example:
passwd- Change a user’s password- Usage example:
passwd username(change the password of the specified user)
- Usage example:
adduser(on some distributions) - A friendlier command for adding users thanuseradd, usually offering more options- Usage example:
adduser newusername(add a new user interactively)
- Usage example:
groupadd- Add a new user group- Usage example:
groupadd groupname(add a new user group)
- Usage example:
groupdel- Delete a user group- Usage example:
groupdel groupname(delete a user group)
- Usage example:
groupmod- Modify a user group- Usage example:
groupmod -n newname oldname(change the user group name)
- Usage example:
gpasswd- Manage the/etc/groupfile- Usage example:
gpasswd -a username groupname(add a user to a user group)
- Usage example:
id- Display a user’s UID and GID as well as the groups they belong to- Usage example:
id username(display information about the specified user)
- Usage example:
who- Display currently logged-in users- Usage example:
who(display all users currently logged in to the system)
- Usage example:
w- Display currently logged-in users and the commands they are running- Usage example:
w(display more detailed information about currently logged-in users)
- Usage example:
su- Switch user identity- Usage example:
su - username(switch to the specified user; the-parameter means the user’s environment variables are also switched to those of the target user)
- Usage example:
sudo- Execute a command as another user (root by default)- Usage example:
sudo command(executecommandas root)
- Usage example:
chown- Change the owner of a file or directory- Usage example:
chown username:groupname file(change the owner and group of a file)
- Usage example:
chmod- Change the permissions of a file or directory- Usage example:
chmod 755 file(change file permissions)
- Usage example:
These commands are the foundation of user and user group management in Linux systems; with them, you can effectively manage user accounts and permissions on the system.
Other Linux Commands
echo- Display a string- Usage example:
echo “Hello,everyone!”(display the string “Hello,everyone!”
- Usage example:
cal- Display a calendar- Usage example:
cal(display the calendar for the current month)
- Usage example:
date- Display and set the system date and time- Usage example:
date 08211550(change the time to August 21st, 3:50 PM)
- Usage example:
clear- Clear the screen- Usage example:
clear
- Usage example:
