my go to bash commands

There are many, many bash commands. But here I’m going to list my go to commands that I use almost on a daily basis. These aren’t commands relating to specific programs, e.g. Jupyter or Conda. Rather they are built in commands that assist me with general tasks. These help you work directly with the system and can make life much easier. Included are any flag options that I think are important to include.

In general, there are several flags that tend to be the same across commands (though not all). Using -r is for doing some recursively, which comes in handy when dealing with directories filled with files. Using -v will show all output while -q will not. A very helpful one is -h which will help you understand the command and its options.
 These are general cases and not guaranteed to be true across different commands. Any variables will be in all upper case. After a general list, certain commands need greater explanation and will be further below.

MOVING AROUND

cd DIRECTORY PATH

move to this directory

cd ..

move up a directory

cd

move to home directory

pwd

print working directory

ls -lha

show all files including hidden ones

tree -L LEVEL

show directory structure with files. use the -L flag to specify how many levels into a directory to go.

PROCESSES

ps aux | grep PROGRAM

find the process ID of a specific program

kill PROCESS-ID

kill a certain program with its process ID

top

see all current processes and their cpu & memory usage as well all cpu & memory usage

exit

exit current process in bash terminal. if no processes are current, it closes the bash window.

reboot

reboot machine

MULTITASKING

screen

create multiple screens within a bash terminal, allowing you to detach processes and close out of the bash terminal and let the process run. also allows multiple bash terminals to occur simultaneously within same window. this is a more complex command and will be covered below.

FILES

touch NEW-FILE

create a new, blank file

cat FILE| more

read file into standard output and pipe it into more allowing you to easily scroll through it. use q to quit out of the more window.

cp FILE NEW-PATH-LOCATION

copy a file to a new location

tar -xvf TAR-FILE

decompress a tar file. the -x flag directs the command to unpack the file while -v makes it a verbose output

unzip ZIP-FILE

decompress a zip file

chmod PERMISSION-NUMBERS FILE

change the read, write, execute ownerships of a file. there is a numbering scheme to tell the command how to change the permissions. this will be detailed below.

CLEANING UP

rm -rf DIRECTORY

recursively remove a directory and all its child directories and files

rm FILE

remove a file

mv OLD-FILE-NAME NEW-FILE-NAME

change the name of a file

REFERENCES

history

see a history of all your bash commands. 

man COMMAND

pull up a manual of the command.

HIDING AND MULTITASKING PROCESSES

screen -s SESSION-NAME

if I open Jupyter Lab, it will have an ongoing process output. if I want to use bash again, I will have to open up a new terminal session window. if I have ssh’ed into a remote machine – such as an AWS instance – and am completing a very long process, turning off my machine will kill that process. to remedy these two problems, we can use screen and its ability to “detach” windows.

we often need to have many processes going on at the same time or need to reference a terminal output. one could have many terminal sessions open. to remedy this, we can use screen and its ability to create new “windows” within a terminal session, name them, switch between them, and even display them within the same terminal session window as different panes.

screen -S SESSION-NAME

  • ctl + a c will create a new window
  • ctl + a ” will list all windows
  • ctl + a A will rename the current window
  • ctl + a S will split current region into two horizontally
  • ctl + a | will split current region into two vertically
  • ctl + a tab will switch windows
  • ctl + X will close current window
  • ctl + Q will close all windows except current one
  • ctl + a d will detach current screen session
#List all detached screens with ID numbers
screen -ls
#Reattach a screen with ID number
screen -r ID-NUMBER

CHANGING PERMISSIONS

chmod NUMBER-SCHEME FILE

the general command goes as follows:

chmod 400 FILE read by owner
chmod 004 FILE read by all
chmod 200 FILE write by owner
chmod 002 FILE write by all
chmod 100 FILE execute by owner
chmod 001 FILE execute by all

each digit place has a meaning:

Digit 1 Sets user or group ID
Digit 2 Sets permissions for file owner
Digit 3 Sets permissions for other users in file group
Digit 4 Sets permissions for other not in file’s group

and each digit value has a meaning:

4 readability
2 writability
1 executability

now if you want to do multiple things, you add all the digits up for each digit place. if you want it to be writable by all, executable only by the owner, and readable only by the group, you get 002 + 100 + 040 = 142

you can also use a letter scheme:

u user who owns
a all
rwx read, write, execute
+,- allow, deny

thus chmod u+rwx means the owner can read, write, and execute.

KILLING AN UNRESPONSIVE PROGRAM

ps aux | grep PROGRAM-NAME

once in a while, a program will become unresponsive and you need to kill it. linux doesn’t have the windows equivalent of task manager or the macOS force quit. instead, in linux you can find the process ID of the program and then use that ID number to kill it.
ps shows a snapshot of current processes. you can pipe it into grep with a name of a program. the name does not have to be totally correct, though it helps. it will then show processes it thinks match the name. it should have a processes ID number to the right of it. typing kill and then this ID number will kill the program.

REPEATING COMMANDS

history NUMBER-OF-LAST-COMMANDS

often in bash, i want to repeat past commands. but I don’t want to have to rewrite the entire command again, especially if it’s long. if you type and enter the history command, it will show you all your past commands. there will be a number to the left of each command. if you type ! and then that number, it will repeat that command for you.