Managing files, links and regex

These are basic common-use, for more check the holy man!

File managing and text processing commands

echo string                     #Prints "string"
echo string > file1             #Overwrites or creates file1 with string 
echo string >> file1            #Creates file1 and/or appends string at the end of file1
echo $PATH                      #Prints contents of variable/environment variable
echo $(command)                 #Prints output of whatever command
echo $(date)                    #Print current date
echo *                          #Prints the name of all items in the current directory
echo */                         #Prints only the name of all directories in the current directory
echo *.txt                      #Prints all items in the current directory ending in .txt
echo \*                         #Escapes and prints literal "*"
echo a \\n b                    #Prints line feed between "a" and "b"
echo a \\c b                    #Produces no further output after \c, so "b" is not printed
echo a \\t b                    #Prints a TAB between "a" and "b"
echo a \\v b                    #Prints a vertical TAB between "a" and "b"
echo 12\\b3                     #Backspaces character just before \\b, will print 13
commandX && echo "Done"         #Will only print "Done" if "commandX" is succesful
commandX || echo "Error"        #Will print "Error" if "commandX" is unsuccesful
echo "\033[1;31mThis is red"    #Prints string after "m" in red color
echo "\033[5;31mFlashing red"   #Prints string in red that will blink constantly
#\033 is for Escape, some colors: [1;33mOrange, [1,34mBlue, [1;35mPurple

Links are special files that point to other files, we distinguish 2 types:

  • Hard Link: points to the data in the disk itself, to the exact inode, if you create a hardlink and check with ls -i you will see that they both point to the same inode index. It's like opening another door to the same place. So if you delete the original file, it will still be there, until all hardlinks are removed the file isn't removed from the disk. Also they can't be pointing to different filesystems(partitions), as inodes would collide since every partition has it's own index. You can only do hardlinks in the same partition. Neither they can point to directories because they could cause errors in correlation. Take in mind that hardlinks share also metadata, so privileges will be the same everywhere in every link, but you can create a hardlink on a public directory pointing to a file on a private directory (ex: your home where no one can access) and they could have access to it while not being able to even see the directory where it comes from.

  • Symbolic Link (AKA Soft Link): points to the path to the data, not the data itself. Unlike hardlinks, when you create a symlink it will use a different inode, they are not the same file per se. So if you delete the original file, the symlink will remain broken, unless you place a file with the same exact name and path again and if you delete the symlink, just create another, no drama. Unlike hard links, they can be used for directories. They are like shortcuts on Windows, a convenient "within reach" pathway to another file/directory. A big positive is that as they only point to a route, you can use them between different filesystems(partitions) or even external storage devices. They usually have lrwxrwxrwx privileges, but its meaningless as they share the privileges of the original file and the directory they are in. Also on a sticky bit directory, only the owner of the link may use it


TAR & Compressing/Decompressing

When sending directories with lots of subdirectories and files to other systems, is best practice to group them to make your life and the one on the other side of the Ethernet cable easier, we have two types:

  • TAR: groups multiple items into a single item, usually called "tarball", it's like an archive but size remains the same.

  • Compress: AKA zipping, uses algorithms to remove redundancy on the items, therefore making them more compact, smaller, so they are easier to move over networks or to get stored

In linux usually what we do is a combination of both, to get all the files grouped and then compressed, best of both worlds.

Remember you have to write the extension ".tar.gz" or ".tgz" into the file, if you forget to it will work anyway, but can cause confussion.

For small to medium files it's not important, but if you are into algorithms or min-maxing space, the overall consensus at 2023 looks like:

zstd(--zstd) > xz(J) > bzip2(j) > gzip(z)

But best compression tends to be slower tho, also gzip is most compatible, so don't worry too much.

Last updated