Difference between revisions of "Useful Linux commands"

From Earlham CS Department
Jump to navigation Jump to search
m (Added more commands)
(Added more commands)
Line 1: Line 1:
 
The one-stop shop for all your Linux command needs. Most commands listed will work on our machines, though that can vary based on installed packages and/or operating systems.
 
The one-stop shop for all your Linux command needs. Most commands listed will work on our machines, though that can vary based on installed packages and/or operating systems.
 +
 +
For almost every command, you can use the --help argument, <code><command> --help</code> to get detailed information about the command as well as a list of its arguments.
  
 
== File and Directory Management ==
 
== File and Directory Management ==
Line 15: Line 17:
 
* <code>cd .</code> and <code>cd ..</code> - The . and .. are used for adjacent movement. <code>cd .</code> "moves" you into the current directory, so basically no movement. <code>cd ..</code> moves you backwards one directory.
 
* <code>cd .</code> and <code>cd ..</code> - The . and .. are used for adjacent movement. <code>cd .</code> "moves" you into the current directory, so basically no movement. <code>cd ..</code> moves you backwards one directory.
 
* <code>cd -</code> - The dash specification takes you to the previous working directory regardless of adjacency.
 
* <code>cd -</code> - The dash specification takes you to the previous working directory regardless of adjacency.
 +
 +
=== pwd ===
 +
Displays the current working directory. This can be usual on certain machines that don't display the full path of your current working directory, like Whedon.
 +
<pre>
 +
root@w0 plugins # pwd
 +
/etc/nagios/plugins
 +
</pre>
 +
In the above example, my current working directory is <code>/etc/nagios/plugins</code>, but the prompt only lists it at nagios.
 +
 +
=== mkdir ===
 +
Used to create directories.
 +
* You can use <code>mkdir directory_name</code> to create it in your current working directory.
 +
* Additionally, you may specify the full path to create the directory somewhere else <code>mkdir /path/to/directory_name</code>.
 +
 +
=== rmdir ===
 +
Used to remove empty directories.
 +
* You can ONLY remove empty directories. You cannot specify recursive or force.
  
 
=== mv ===
 
=== mv ===
Line 21: Line 40:
 
* To rename a file, you can "move" it into a new one. <code>mv old.txt new.txt</code> will put the contents of old.txt into the newly created new.txt. This action will delete old.txt. NOTE: This WILL overwrite the file if you move it into a preexisting one.
 
* To rename a file, you can "move" it into a new one. <code>mv old.txt new.txt</code> will put the contents of old.txt into the newly created new.txt. This action will delete old.txt. NOTE: This WILL overwrite the file if you move it into a preexisting one.
 
* <code>mv -i</code> - If you're worried about overwriting files, you can use the -i specification. This will prompt you for confirmation before overwriting.
 
* <code>mv -i</code> - If you're worried about overwriting files, you can use the -i specification. This will prompt you for confirmation before overwriting.
 +
 +
=== rm ===
 +
Used to remove files and/or directories. Unlike <code>rmdir</code>, you ''can'' use <code>rm</code> to remove directories. Even ones that aren't empty.
 +
* <code>rm -d</code> - Essentially the same as <code>rmdir</code>; it can only remove empty directories.
 +
* <code>rm -r</code> - Recursively removes files and directories from a directory, including itself.
 +
* <code>rm -rf</code> - The same process as <code>rm -r</code>, except it bypasses any locks or protections on files and deletes them. Only use this command if you are certain you understand what you're deleting, ''especially'' if you're '''root'''.

Revision as of 13:36, 23 March 2025

The one-stop shop for all your Linux command needs. Most commands listed will work on our machines, though that can vary based on installed packages and/or operating systems.

For almost every command, you can use the --help argument, <command> --help to get detailed information about the command as well as a list of its arguments.

File and Directory Management

ls

Lists all files and directories

  • ls -a - List all files, including hidden ones like .ssh, .bashrc, and so on.
  • ls -l - Include extra file information like permissions, owner, groups, modified date, and more.
  • ls -s - List size of files in blocks.
  • ls -S - List by size, largest first.

cd

Used to change the directory.

  • cd - When no path is specified, you will move to the user's home directory. As root, you will move to /root/. As your user, you will move to /eccs/home/<username>/.
  • cd /path/to/file - When changing directories, if you're not in the immediate parent directory, you need to specify the full path. For example, if my current working directory is /etc, I can cd jupyterhub since it's within /etc. However, if my current working directory is /bin, I would have to specify the full path for /etc/jupyterhub.
  • cd . and cd .. - The . and .. are used for adjacent movement. cd . "moves" you into the current directory, so basically no movement. cd .. moves you backwards one directory.
  • cd - - The dash specification takes you to the previous working directory regardless of adjacency.

pwd

Displays the current working directory. This can be usual on certain machines that don't display the full path of your current working directory, like Whedon.

root@w0 plugins # pwd
/etc/nagios/plugins

In the above example, my current working directory is /etc/nagios/plugins, but the prompt only lists it at nagios.

mkdir

Used to create directories.

  • You can use mkdir directory_name to create it in your current working directory.
  • Additionally, you may specify the full path to create the directory somewhere else mkdir /path/to/directory_name.

rmdir

Used to remove empty directories.

  • You can ONLY remove empty directories. You cannot specify recursive or force.

mv

Used to move and rename files.

  • This command uses a mv /path/to/file /path/to/destination syntax. It follows the same rules as cd in that you need to specify the full path unless moving to the current working directory or an adjacent directory.
  • To rename a file, you can "move" it into a new one. mv old.txt new.txt will put the contents of old.txt into the newly created new.txt. This action will delete old.txt. NOTE: This WILL overwrite the file if you move it into a preexisting one.
  • mv -i - If you're worried about overwriting files, you can use the -i specification. This will prompt you for confirmation before overwriting.

rm

Used to remove files and/or directories. Unlike rmdir, you can use rm to remove directories. Even ones that aren't empty.

  • rm -d - Essentially the same as rmdir; it can only remove empty directories.
  • rm -r - Recursively removes files and directories from a directory, including itself.
  • rm -rf - The same process as rm -r, except it bypasses any locks or protections on files and deletes them. Only use this command if you are certain you understand what you're deleting, especially if you're root.