More Linux
& after the command is for running it in the background. && lets you do something based on whether the previous command completed successfully || which is the logical or, and also ; which is just a separator which doesn't care what happend to the command before.
$ false || echo "Oops, fail"
Oops, fail
$ true || echo "Will not be printed"
$
$ true && echo "Things went well"
Things went well
$ false && echo "Will not be printed"
$
$ false ; echo "This will always run"
This will always run\ allows us to use the next line
$ uname \
> -r
4.4.0-18362-MicrosoftWhy do we use ./ to execute a file in Linux?
./ to execute a file in Linux?Because the current directory isn't in your $PATH . This is mainly for security reasons.
Sourcing and Executing
source
sourcesource or . (with a space afterwards) will execute the file in the current environment. This explains why we source the ~/.bashrc file, so that it will export variables and alias in the current environment.
or
executing
to execute we do the following ./a.out. This is executed as a child process. Therefore when the child terminates the parent doesn't have access to the changes
export
exportThe export command provides the ability to update the current shell session about the change you made to the exported variable
export -p view all variables
export name[=value]
Another cool thing we can do is instead of running each time our compiled program like this ./a.out we can do this (it may be dangerous, dont try this at home)
To export a function we use the -f flag like so: export -f foo
To remove an exported variable we use unset:
alias
aliasAliases are like custom shortcuts used to represent a command. They are only stored in the current shell instance and will not be saved later on. They instruct the shell to replace one string with another string while executing the commands.
Creating an alias
example 1:
example 2:
example 3: start shortcutting everything. Imagine that instead of write git clone or git push we could do something like this
Listing aliases
Removing aliases
Creating Permanent Aliases and Variables
To permanently save our aliases we need to edit the ~/.bashrc file
Be very careful when editing this file!
Open the file,edit and save the file. Then type to execute the changes
source is the same as .
Also, If you want to put the environment for system-wide use you can do so with /etc/environment file. There you'll find a PATH variable. But it is more recommended and easy to just edit your .bashrc file.
Here is a list of some environment variables:
$PATH- list of dirs your system looks for executable files$HOME$USER
Now if we edit and source our .bashrc file to include in a path lets called my_dir where the program is located and we have a program there called super_cool_program
we can do the following from any directory
export/variables vs alias
export/variables vs aliasOne difference between the two is that aliases are only a shell feature, can only be used as the names of programs to run, i.e. as the first word in a command line. Environment variables (export) are inherited by all subprocesses/children (unless deliberately cleared).
cp
cpcp can copy files or folders. there are mainly 3 cases:
Two file names - copies one file to the next file
One or more arguments - copy file/s to directory
or
Two directory names - copies all files of the source directory to the destination directory
the
-Rflag is to copy recursively
Flags
-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
-v : Verbose output.
-r : Copy directories recursively.
-i : Interactive, will ask if to overwrite
Instead of cp you can also use rsync
mv
mvmv can move files or rename them. there are mainly 2 cases:
Here we rename file
Here we copy files to directory
Flags
-n : prevent overwriting
-i : Interactive, will ask if to overwrite
-f : forcefully overwrite
-u : move only when the file is newer
wget command
wget commandThe wget command allows us to download files
Syntax: wget url
Multiple files
wget url1 url2
flags
-O(uppercase) : the output name of download.-O new_name-b: download in background.-b log_file_name
curl command
curl commandVery good at testing REST api and also downloads.
Will fetch the html page
flags
-o,--output: the output name of download.-O new_name
download into specific folder
curl vs wget
curl vs wgetwget's major strong side compared tocurlis its ability to download recursively.curlsupportsFTP,FTPS,HTTP,HTTPS,SCP,SFTP,TFTP,TELNET,DICT,LDAP,LDAPS,FILE,POP3,IMAP,SMTP,RTMPandRTSP.wgetsupportsHTTP,HTTPSandFTP.curlbuilds and runs on more platforms thanwget.curloffers upload and sending capabilities.wgetonly offers plain HTTP POST support.
Benchmarking
The simple way to benchmark is to use the time command
real (wall clock time) is the time from start to finish of the call. It is the time from the moment you hit the
Enterkey until the moment thewgetcommand is completed.user - amount of CPU time spent in user mode.
system or sys - amount of CPU time spent in kernel mode
More reliable benchmarking
Lets run a.out which will perfom a for loop 1 billion times
The -r 10 will run your app 10 times and do statistics over it. -d outputs some more data, such as cache misses.
Extreme benchmarking
sudo chrt -f 99 runs your benchmark in FIFO real-time class with priority 99, which makes your process the top priority process and avoids context switching
Compression and Archiving
.tar : uncompressed archive file
.zip : (usually) compressed archive file
.gz : file (archive or not) compressed using gzip
gzip
Compression: gzip filname
Decompress:
tar
if we have 2 files: file1.txt, file2.txt we can archive them like this
or for all txt files
Explanation: of cvf
c- means create
-v, --verbose - display output in terminal
f - file options
To extract
x - extract
Archiving and Compressing
-z, --gzip, --gunzip : This option tells tar to read or write archives through gzip. This option should be used, for example, when operating on files with the extension .tar.gz.
Decompressing
Last updated