Skip to main content

Files, Pipes, & Permissions

Reading Files

less

  • Only Read the file in terminal.
  • for quite type q and hit enter
  • newer version of more
less fileName

more

  • only read the file
  • older version the best is less
more fileName

cat

  • does is read the entire file and output it
cat fileName

man

  • If you run man less it will show you the manual for less.

grep

  • Grep is just like a thing to like look for other things.
  • Grep command can be used to find or search a regular expression or a string in a text file.
  • Read More
grep "Linux" welcome.txt

Creating & Moving Files

mkdir

  • mkdir makes a new directory/folder
mkdir my-new-folder
  • Another useful thing is the -p (for parents) flag. Run mkdir -p a/lot/of/folders and it will create all those folders as necessary.
mkdir -p a/lot/of/folders

rm

Remove a file! Be very careful, this program has got no chill. You tell it delete it everything and it will oblige you.

  • Ot will remove the one file
rm new-file.txt
  • It will remove the directory
rm -r my-new-folder
  • -f flag to force everything through without confirmation.
rm -rf my-new-folder

touch

Touch create a new, empty file. If I say touch new-file.txt it'll create a new file called new-file.txt right where I am. If new-file.txt already exists then it just changes the last modified and last accessed time.

touch new-file.txt

cp

  • cp is short for copy and does just that.
  • If you want copy a file you run blow command
run cp source-file.txt destination-file.txt
  • If you want to copy a whole folder and everything in it, use blow command to recursively copy everything from one place to the other.
cp -R source-directory destination-directory

mv

  • mv stands for move.
  • move a file from one place to another, or how you rename a file (which is still moving it in some sense.)
  • Try running touch file.txt then mv file.txt new-name.txt.
  • Unlike cp, with mv you don't to do anything special to move a folder. Just do mv folder-name new-folder-name and it all works.

tar

tar is short for tape archive and it initially used to prepare files to be backed up to a magnetic tape archive but it became useful to just group together files in single files as a tarball (like a zip file.)

  • Let's say we wanted to put three files and a folder into a single tarball. Run the following
mkdir folder1
touch file1.txt file2.txt folder1/file3.txt
tar -cf archive.tar file1.txt file2.txt folder1
ls -lsah

You should now see a file called archive.tar. This is a single file that contains the above files. You should ship this off to your friend and they'd have the same three files with that directory when they un-tar'd it.

However, this archive isn't compressed. It's literally just the files stuck together. Normally we'd want to compress this as well and tar makes it really easy to. If you just tack on the -z flag, it'll automatically compress it. So try this

tar -zcf archive.tar.gz file1.txt file2.txt folder1
ls -lsah

Notice archive.tar.gz is significantly smaller than archive.tar. This is because it got run through gzip.

So let's unpack one of these. We'll do the gzip'd one because 99.99% of the time it should be compressed.

mkdir destination-folder
tar -xzf archive.tar.gz -C destination-folder/

Notice we swapped c for x in the flags. This is because we went from creating to extracting. And then the -C is just giving it a destination folder to extract it. You can leave that off but it'll just extract the archive where-ever you are.

Wildcards & Replacements

Let's you wanted to remove all the .txt files in your directory. You could say rm file1.txt file2.txt file3.txt <etc> but that's time consuming.

  • You know they all end with .txt, wouldn't it be nice if you could say "remove anything that ends in .txt". you can using the wildcard * is for with paths.
  • it'll remove everything matches that pattern (file that extension is .txt).
  • * is match the before and after is optional if available then also both match.
rm *.txt

Okay, so say you wanted to match file1.txt and file2.txt but not file.txt. file*.txt will match all three of those. For that you can use ?. So try file?.txt.

Try this

touch file1.txt file2.txt file.txt
ls file*.txt
ls file?.txt
Notice

The first ls call does output file.txt and the second one doesn't.

  • ? represents exactly one character.
  • you can use [] to limit your characters too.Let's say you wanted 1-5. So you could say.
ls file[1-5].txt

Expansions

Let's say you wanted to create three files using touch all at once. You could do:

touch file4.txt
touch file5.txt
touch file6.txt

It's a bit annoying but it'll work. But try this instead

touch file{4,5,6}.txt
touch {Aisha,Lanie,Joumana,Krista}-profile.txt

Even better, try this:

touch file{0..10}.txt
echo {a..z} # prints a to z
echo {z..a} # reverse order
echo {0..100..2} # prints every other (aka even) number from 0 to 100
echo {100..0..5} # prints every 5th number in reverse order from 100 to 0
echo {a..z}{1..5} # prints out a1 a2 a3 a4 a5 b1 b2 b3 <etc>

Pipes

Stream

You have to assume that the output of any program could be the input to another program.And the way that this is accomplish lead us to things to kinda like connected together through something called streams in pipes. One stream of characters andyou'll pipe that into another stream of characters.

  • Print the text.
echo "this is simple text print on the terminal"
  • Adding the text or stream the text into new file
echo "adding this text into file" 1>new-file.txt
  • Adding one file content into another file
cat new-file.txt > transfer-old-file-data.txt
  • What if we write double arrow that mean that append the data into the other file.The reason is that single arrow > can replace all of the content.
cat new-file.txt >> transfer-old-file-data.txt

Pipes

Everything that I've been talking about sofar has been specifically dealing with going in and out to files.But what if you wanna go from program to program?That's called a pipe, right? So we're piping one thing to another.

Permissions

Principle of Least Power

whoami  # print the username of your system
  • we can actually see the name of all of the users on our computer at this particular time, right?
  • So cat passwd is actually a file that exists in your computer that keeps track of all of your various different users.So we we have daemon, bin, sys, sync, man, lp, mail.
cat /etc/password/
  • You want to give these accounts the least amount of privilege thatthey can have to be able to make changes that still allows them to do their job.
  • What if we go the root directory hassanali:~$ and create a new folder mkdir 'hi'. then its permission denied. But why? The root user is the only one that has any sort of privilege in this root directory to be able to do anything,

Superuser

I'm here in the root directory and I really wanted to make that folder.How would I do it? Well, the owner user that can do it is the super user.The super user is also the other name for the root user.

  • use sudo before the command then you can act like a superuser.

Adding a New User

New user can be added by using the useradd command. You can give the permission by adding them into the groups. So now this is not have any permission. We have adding them into the group blow group section.

sudo useradd brian
sudo passwd brian
# make a password, I made something simple like "asdf"
su brian
# your new password
whoami
sudo su
# brian is not in the sudoers file. This incident will be reported.

Deleting a User

  • sudo mean "switch user and do"

Let's try deleting that brian user we added.

sudo userdel brian

Groups

  • This is useful because like imagine you run a server and that server has 100 users on it. By giving individual privilege is not a good way so you can use the groups.
  • Make a group and assign the privilege to that group.adding multiple user on those group and all the user on the same group has access same privilege.
  • So it just makes it easier to add and manage all those people.
  • Like you could create like a readers group and the readers group has access to read all the files, but I can't write to it and they could have another writers group.
  • It would add the new group to brian. And he's going to be added to the pseudo group and the sudo group is allowed to sudo.

Each part of the command does:

  • sudo is the command that allows you to run a command as a super user.
  • usermod is the command that allows you to modify a user.
  • -aG is the flag that allows you to add a user to a group.
  • sudo is the group that you're adding the user to.
  • brian is the user that you're adding to the group.

Add the user brian to the sudo group:

sudo usermod -aG sudo brian
## Another command
sudo usermod --append --groups sudo brian
  • Switch the user
su brian

Permissions

Right now I'm logged in as brian but I'm in ubuntu's home directory Now, as brian, run touch brian.txt. You should see touch: cannot touch 'brian.txt': Permission denied. That's because everyone's home directory is locked down to themselves, so this is working as we anticipate.
Use the sudo command to create the file. Then its create the file, without permission denied.

sudo touch brian.txt
  • ls -lsash will show you the permissions of the files in the directory.
ls -lsah
4.0K -rw-rw-r--   1 hassanali hassanali 1.3K Jun 28 15:11 package.json
560K -rw-rw-r-- 1 hassanali hassanali 558K Jun 28 15:11 package-lock.json
4.0K -rw-rw-r-- 1 hassanali hassanali 768 Jun 13 15:47 README.md
4.0K -rw-rw-r-- 1 hassanali hassanali 1.3K Jun 27 16:45 sidebars.ts
4.0K drwxrwxr-x 3 hassanali hassanali 4.0K Jun 28 12:49 blog
4.0K drwxrwxr-x 8 hassanali hassanali 4.0K Jun 27 16:45 docs
  • drwxr-xr-x is the first part of the output.
    1. The first character is d which means that this is a directory. The next three characters are rwx which means that the owner of this directory can read, write, and execute it.
    2. The next three characters are r-x which means that the group that owns this directory can read and execute it.
    3. The final three characters are r-x which means that everyone else can read and execute it.
  • rw-r--r-- is the first part of the output.
    1. The first character is - which means that this is a file.
    2. The next three characters are rw- which means that the owner of this file can read and write it.
    3. The next three characters are r-- which means that the group that owns this file can read it.
    4. The final three characters are r-- which means that everyone else can read it.
  • rwx here means that this can be read, written to or executed by Ubuntu.