Difference between revisions of "Useful Linux commands"

From Earlham CS Department
Jump to navigation Jump to search
m (find)
m (Need to add symbolics and special cases to chmod)
Line 95: Line 95:
 
   </blockquote>
 
   </blockquote>
  
 +
== File Permissions and Ownership ==
 +
=== chmod ===
 +
Used to add, remove, or change file permissions. You normally specify permissions using an octal format, but you may also use symbolic as well. Here are some common chmod commands; full access for a user implies read, write, and execute ability:
 +
* <code>chmod 600</code> - The owner can read/write, but there is no access for others. Some use cases are private files and SSH keys.
 +
* <code>chmod 644</code> - The owner can read/write, but others can only read. Useful for config files and public web files.
 +
* <code>chmod 664</code> - The owner and group can read/write, but there is no access for others. Useful for files used specifically in group collaboration. 
 +
* <code>chmod 700</code> - The owner has full access, but there is no access for others. Useful for private scripts (since you need the ability to execute a script) and sensitive directories.
 +
* <code>chmod 755</code> - The owner has full access, but others can only read and execute. Useful for certain executables and web directories.
 +
* <code>chmod 775</code> - The owner and group have full access, but others can only read and execute. Useful for group-managed directories.
 +
* <code>chmod 777</code> - Everyone, and I mean ''everyone'', has full access. So...don't use this unless ''absolutely'' necessary.
  
  
 
-- Major Overhaul: March 2025 by Tyler Jones [tdjones22] --
 
-- Major Overhaul: March 2025 by Tyler Jones [tdjones22] --

Revision as of 19:58, 28 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.
  • The * isis a powerful wildcard in Linux that allows you to match multiple files efficiently when using commands like rm. For example, take a look at this directory:
tdjones22@bowie:~/directory$ ls
123.txt  131.txt  14.txt  1file.txt  208.txt  263.txt  62.txt code.py code.rb config.conf

Let's say you want to remove every file that starts with the number 1. You can use rm *1 to remove any file matched with the first character being 1.
Files left after the command:

208.txt  263.txt  62.txt code.py code.rb config.conf

Let's say you want to remove files with a specific extension. You can use rm *.txt to remove every file with the .txt extension.
Files left after the command:

code.py code.rb config.conf

cp

Used to copy files and/or directories. Similar to rm in that you need to specify recursive when copying a non-empty directory. It follows a cp /path/to/file /path/to/destination syntax.

  • cp -r - Recursively copies a directory; the only way, using cp, to copy a directory that isn't empty.

find

A tool to find files using many criteria including name, type, contents of file, and more. A common syntax for this command is find /starting/directory -specification "name_of_file.txt".

  • find / -name "name.txt" - A typical usecase for find is searching by file name. If I've lost track of a file named found_file.txt, I can search the entire machine for it using this command:
tdjones22@bowie:~$ find / -name "found_file.txt" 2>/dev/null
/earlhamcs/eccs/users/tdjones22/directory/found_file.txt

This command will usually take some time to execute fully, so don't be alarmed when it hangs for a few seconds.


The starting directory was specified as / which is just the parent directory, meaning it searches the entire file system. The search type was specified as -name, which looks for matching named files. You can additionally add 2>/dev/null to silence all of the permission errors you'll get when searching all of the machine's directories.

  • find / -name "*.txt" -exec grep -iIl "contents to search for" {} + 2>/dev/null - If you know a portion of the contents of a file, but you don't know the name, you can use find in tandem with grep to find it. Though, it's far easier and more convenient to only use grep.

Let's say I have a missing text file, but I know it contains "solar power", I can find it like so:

tdjones22@bowie:~$ find / -name "*.txt" -exec grep -RiIl "solar power" {} + 2>/dev/null
/eccs/home/tdjones22/test_find.txt

Like the previous command, this will take a hot minute to complete.


The starting directory was specified as / which is just the parent directory, meaning it searches the entire file system. We're looking for a file by its name specified as "*.txt", which looks for any files with the .txt extension. The -exec grep -iIl "solar power" {} + section is what searches through the file's contents. The grep arguments specify the following: -l - ignore case, -I - ignore binary files, and -l - display file name. You can additionally add 2>/dev/null to silence all of the permission errors you'll get when searching all of the machine's directories.

File Permissions and Ownership

chmod

Used to add, remove, or change file permissions. You normally specify permissions using an octal format, but you may also use symbolic as well. Here are some common chmod commands; full access for a user implies read, write, and execute ability:

  • chmod 600 - The owner can read/write, but there is no access for others. Some use cases are private files and SSH keys.
  • chmod 644 - The owner can read/write, but others can only read. Useful for config files and public web files.
  • chmod 664 - The owner and group can read/write, but there is no access for others. Useful for files used specifically in group collaboration.
  • chmod 700 - The owner has full access, but there is no access for others. Useful for private scripts (since you need the ability to execute a script) and sensitive directories.
  • chmod 755 - The owner has full access, but others can only read and execute. Useful for certain executables and web directories.
  • chmod 775 - The owner and group have full access, but others can only read and execute. Useful for group-managed directories.
  • chmod 777 - Everyone, and I mean everyone, has full access. So...don't use this unless absolutely necessary.


-- Major Overhaul: March 2025 by Tyler Jones [tdjones22] --