Linux 101 (NE classes, Udemy classes etc.) Flashcards
How to append the user to a secondary group?
usermod -aG [group] [username]
How to see all users and groups in the system?
/etc/passwd, /etc/group
How to give a group root privileges?
via adding the %groupname ALL=(ALL) ALL line to the /etc/sudoers file
Meaning: “any members of group groupname on ALL hosts can sudo to ALL user accounts to run ALL commands.
How to change ownership to a file/directory?
How change group ownership of a file with the other command?
chown [user].[group] [file]
or
chown [user]:[group] [file]
chgrp [group] [file]
How to change file/dir permissions? (2 methods)
Symbolically: chmod u=rwx,g+rw etc..
Numerically: chmod 753
How to locate a path to a given command?
which [command]
How to search the manual for keywords
man -k [keyword]
How does umask work?
By substracting numerically the permissions from any given file, od dir. So if umask is 777, the new directory will have 000 permissions. For files umask substract from 666 instead of 777 as it is for directories.
How to move, copy , rename directories (that have something inside of them)?
mv [dir] [destination]
cp -r [dir] [destination]
mv [old_name_dir] [new_name_dir]
How to put a directory into tar file with compressing?
tar -czf [new .tar file name] [directory]
How to compress, and uncompress files using gzip?
gzip [filename]
gunzip [filename]
How to view contents of .gz files? How to view content of tar files? (In human readable format, no cat command)
zcat [file]
tar -tf [file]
How to find a file (that is in your working directory) that starts with a “b”, then has two random letters, fourth letter is either “g” or “f”, fifth is a number from 1 to 6, sixth is neither “g” or “f”, and it ends with .txt?
ll -d (or ll | grep…) b??[g,f][1-6][!g,f]*.txt
How to redirect output of a text file to some other file replace it’s contents, and not show error message in a single command?
cat [file1] [file that doesn’t exis]> > [file2] 2> /dev/null
How to redirect output of a text file to some other file with replacing it’s contents, and not show error message in a single command?
cat [file1] [file that doesn’t exist ] > [file2] 2> /dev/null
How to redirect output of a text file to some other file and append it to it’s contents, and redirect error message to that same file? In a single command
cat [file1] [file that doesn’t exist]»_space; [file2] 2>&1
How to compare files in two ways?
diff [file1] [file2]
sdiff [file1] [file2]
How to display filetype?
file [file]
How to cat with number of lines displayed?
cat -n
How to display content of a binary file in a human readable form?
strings [file]
How to cut a selected portions of a file? Let’s say we want to print only name of a user, and his login bash from the /etc/passwd file.
grep [username] /etc/passwd | cut -d: -f1,7
How to customize shell prompt, and make the shell customization saved between sessions?
The shell customization information is storen in the PS1 env variable, and it content of that env could look like this: [ \t \u@\h \W]$
Most likely we want the customization options to be saved in between sessions, so we would have to put this line into the /~/.bash_profile:
export PS1=”[customization options]”.
This would load up those PS1 options everytime we login into interactive shell.
How to create an alias?
alias [aliasname]=[value]
How to make alias persist between sessions?
either put your alias into the /~/.bash_profile or /~/.bashrc file.
How to delete alias? How to see one’s contents? How to see all user’s aliases?
unalias [aliasname]
alias [aliasname]
alias
How to see all environment variables?
printenv
How to view environment variable content?
printenv [env name]
or echo $[env name]
How to create environmental variable?
export [variable]=[value]
How to remove enviromental variable?
unset [variable name]
How to keep enviroment variables persist between sessions?
put your export variable command into /~/.bash_profile file
export [var]=[value]
How to display current processes in the system?
ps
How to display all processes in the system in the interactive way?
top
How to kill a process?
kill [process name or PID]
How to send a signall to a process?
kill [-signal id] [PID or process name]
How to display a list of signals which we can send to a process?
kill -l
How does the cronjob format look like? Give an example for running something every tuesdayt at 09:15 pm
[minute] [hour] [day of a month] [month of a year] [day of a week]
15 21 * * 2 /bin/pwd
How to create a cronjob, list cronjobs, remove and edit them?
crontab [filename]
crontab -l
crontab -r
crontab -e
How to change users with and without changing user’s enviroment and what does it mean?
So let’s say we are root user, and we want to change to “xyz” user with changing our enviroment into the xyz enviroment. We would have to use su - xyz. If we didn’t want to change enviroment, we would have to use su xyz, so the same as above but without “-“. If we do change enviroment when changing user, it just means that we will have the xyz users aliases, enviroment variables etc. If we wouldn’t, we would still have roots aliases, variables, etc.
How to see what user we are?
whoami
How to run a command as other user?
su -c ‘[command]’ - [user]
How to run programs as other user?
sudo -u [user] [program]
Where is shell history stored?
/~/.bash_history and we can see current history with “history” command, as bash_history files only stores commands from last user session.
How to change history size?
By editing $HISTSIZE, so
export HISTSIZE=[number]
How to run commands by their history number?
![number]
How to run previous command?
!!
How to run last commands that starts with “xyz”?
!xyz
How to grab arguments from previous commands?
!:[number of argument]
How to search for package in the network?
yum search [keyword]
How to install a package?
yum install [package]
How to uninstall a package?
yum remove [package]
How to install a package that we have on our system already?
rpm -i [path to package]
How to check filesize? How to check disk usage?
filesize - du
disk usage - df
How to copy files over the network?
Using either SCP (secure copy) or SFTP (ssh file transfer protocol) or ftp.
Using graphical clients: eg. Cyberduck, Filezilla.
how to start a command in background?
[command] &
How to kill a foreground process and how to suspend one?
ctrl-c - kill
ctrl-z - suspend
What is a foreground process and a background process?
Foreground processes refer to applications you are running that you are currently interacting with, and which applies equally to graphical user interfaces as it does to the command line. Background processes refer to applications that are running but not being interacted with by the user.
how to background a suspended process?
bg [%num]
how to foreground a background process?
fg [%num]
how to list jobs in linux?
jobs
what’s the difference between processes and a job in linux?
A process is any running program. A job is something started at the command line and is attached to that shell. So, all jobs are processes, but not all processes are jobs.