Super fast introduction to UNIX/Linux...command line
Some commands you can try to see what happens in the command line and how it looks like.
!# name of the user
!whoami
!# name of the computer
!hostname
!# manual for the command; press q to quit
!man hostname
Everytime you work in the command line you will have to work with paths and directories (folders in Windows). You can go from a directory to a directory without opening the graphical browser - much faster! You can also copy files and directories.
!# what is the path to the directory where I am now; Path of Working Directory
!pwd
Be careful, commands are case-sensitive!
!# doesn't do anything and possibly throws an error that it doesn't know the command (simply it doesn't exists unless there is a different command with very similar name - might happen)
!PWD
We can try using the absolute path and relative path
We would like to go one directory up from our current 'location'.
!# where we are now
!pwd
!# what is the content of the current directory
!ls
!# go one directory up using the absolute path - it goes from the root of the computer - from the very 'starts'
%cd /home/guest03/Desktop
!pwd
!ls
# we can also use a relative path - it goes relative to the current directory
%cd ../
!pwd
!ls
We can also list a directory without even going there.
!ls /home/guest03/Desktop
There is also a special sign ~
(tilde) that marks your home directory. Home is basicaly your default directory.
!# using a special sign to go to your home directory
%cd ~
!pwd
Of course, using the command line you can copy files from one directory to another, create new ones, delete, rename, and everything else you can imagine.
!# first, create an empty, new directory
!mkdir newdir
!# do we really have it there?
!ls
!# go to the new directory (relative path) and create a new empty file there
%cd newdir/
!touch newfile
!ls
Now, we can move the file using again both relative and absolute paths.
!# move the file to a different directory using the absolute path
!mv newfile /home/guest03/Downloads
!ls /home/guest03/Downloads
!ls
!# and now we can move it back in here using the relative path
!mv /home/guest03/Downloads/newfile .
!ls
!ls /home/guest03/Downloads
We can also copy and rename the file.
!# copy (not move) the file
!cp /home/guest03/newdir/newfile ../Pictures/
!ls
!ls ../Pictures/
!# copy the file and rename it
!cp ../newdir/newfile ../Pictures/anotherfile
!ls ../Pictures/
!# in a simillar manner we can move the file
!mv newfile ../Pictures/movedfile
!ls
!ls ../Pictures/
!# renaming a file in the command line is the same as moving the file
%cd ../Pictures/
!ls
!mv anotherfile renamedfile
!ls
!# and we can remove a file
!rm renamedfile
!ls
You can do the same things with directories as with files but we need some minor changes.
!# make new directory
!mkdir newdir
!ls
!# copy directory - have to use RECURSIVE copying
!cp newdir ../
!cp -r newdir ../
!ls ../
!# move the directory
!mv newdir /home/guest03/Downloads/
!ls
!ls /home/guest03/Downloads/
!# we can remove an empty directory
!rmdir /home/guest03/Downloads/newdir
!ls /home/guest03/Downloads
But if the directory is not empy, simple rmdir won't work. We have to use RECURSIVE delete. But be careful, command line doesn't ask "Are you sure?" but does it right away.
!# remove non-empty directory
!mkdir bagr
!touch bagr/file
!ls
!rmdir bagr
!rm -r bagr
!ls
Special making directories - more than one "level". We can also create a long "list" of directories which are in a sequence one after the other. For this, we have to create all the parent directories of the last listed directory = all directories which are "above" the last one.
!# make all the directories even if some of them doesn't exist
!mkdir /home/guest03/Desktop/testdir1/testdir2
!mkdir -p /home/guest03/Desktop/testdir1/testdir2
!ls /home/guest03/Desktop/testdir1
Now, we can see how to download a file, how to read it and how to copy it to a different file (differently than the previous time).
First, we have to download a file. There are numerous ways how to do it but this one is one of the most easiest.
!# download a file from the command line
!wget https://www.dropbox.com/s/7ja4d2kifo3cbqx/textfile.txt?dl=0
!# see if we have it
!ls
!# see the content of the file
!cat textfile.txt\?dl\=0
!# fix the ugly name
!wget https://www.dropbox.com/s/7ja4d2kifo3cbqx/textfile.txt?dl=0 -O textfile.txt
!# see if it's there and delete the old one
!ls
!rm textfile.txt\?dl\=0
!# how many lines do we have
!wc -l textfile.txt
!# what is the beginning of the file (top 10 lines)
!echo "Top 10 lines."
!head textfile.txt
!# what is the end of the file (last 10 lines)
!echo "Last 10 lines"
!tail textfile.txt
We can also edit the files right from the command line. There is a number of tools for this purpose but one of the most easiest one is nano
or pico
. Unfortunately, this editing doesn't work in Jupyter and we have to open a command line for this. Please, do this now.
Edit our text file using nano
.
nano textfile.txt
Now you can do whatever you want, change, delete, etc. You can also see a 'help' bar at the bottom.
Once you are done you can save the file by pressing control+o
, typing a new name of the file or saving to a same file.
nano
can do a lot of other stuff. Just see the manual.
It might happen that the computer won't have nano
nor pico
installed. In that case you have to use the 'hardcore' tools such as vi
or vim
but this I will leave to you.
We will mainly use the for loop.
!# this loops goes thourgh all files that have a ".txt" suffix and prints their name and first 10 lines
for file in *.txt
do
echo $file
head $file
done
Note the * character. This is one of the regular expressions. The * stands for any number of any characters. This means there could be any name of any length of the file but it just has to finish with ".txt".