Introduction to the Terminal
Disclaimer: This post has been translated to English using a machine translation model. Please, let me know if you find any mistakes.
Post Format
To avoid having to post console images for every action I take, I have created the following function that receives the terminal command we want to execute and returns the output that the terminal would give us.
Sometimes I will use this function, and other times I will use !
before each command, which in notebooks means that you are going to run a terminal command.
import subprocessimport oslast_directory = ''def terminal(command, max_lines_output=None):global last_directorydebug = Falsestr = command.split()# Check if there are " or ' charactersfor i in range(len(str)):if debug: print(f"i = {i}, str[i] = {str[i]}")if len(str[i]) > 0:if str[i][0] == '"' or str[i][0] == "'":for j in range(i+1,len(str)):if debug: print(f" j = {j}, str[j] = {str[j]}")if str[j][-1] == '"' or str[j][-1] == "'":for k in range(i+1,j+1):if debug: print(f" k = {k}, str[i] = {str[i]}, str[k] = {str[k]}")str[i] = str[i] + " " + str[k]if debug: print(f" k = {k}, str[i] = {str[i]}, str[k] = {str[k]}")str[j:] = [""]str[i] = str[i].replace('"','')# Remove empty stringsstr = [x for x in str if x != ""]if debug:print(str)returnif str[0] == "cd":last_dir = os.getcwd()if len(str) == 1:os.chdir('/home/wallabot')else:if str[1] == "-":os.chdir(last_directory)else:os.chdir(str[1])last_directory = last_direlse:result = subprocess.run(str, stdout=subprocess.PIPE).stdout.decode('utf-8')if max_lines_output is not None:result_split = result.split(' ')print(' '.join(result_split[:max_lines_output]))print(" ...")print(' '.join(result_split[-5:]))else:print(result)
First commands to navigate the terminal
ls
(list directory)
The first command we are going to see is ls
(list directory) which is used to list all the files in the folder we are in.
import subprocessimport oslast_directory = ''def terminal(command, max_lines_output=None):global last_directorydebug = Falsestr = command.split()# Check if there are " or ' charactersfor i in range(len(str)):if debug: print(f"i = {i}, str[i] = {str[i]}")if len(str[i]) > 0:if str[i][0] == '"' or str[i][0] == "'":for j in range(i+1,len(str)):if debug: print(f"\t j = {j}, str[j] = {str[j]}")if str[j][-1] == '"' or str[j][-1] == "'":for k in range(i+1,j+1):if debug: print(f" k = {k}, str[i] = {str[i]}, str[k] = {str[k]}")str[i] = str[i] + " " + str[k]if debug: print(f" k = {k}, str[i] = {str[i]}, str[k] = {str[k]}")str[j:] = [""]str[i] = str[i].replace('"','')# Remove empty stringsstr = [x for x in str if x != ""]if debug:print(str)returnif str[0] == "cd":last_dir = os.getcwd()if len(str) == 1:os.chdir('/home/wallabot')else:if str[1] == "-":os.chdir(last_directory)else:os.chdir(str[1])last_directory = last_direlse:result = subprocess.run(str, stdout=subprocess.PIPE).stdout.decode('utf-8')if max_lines_output is not None:result_split = result.split('\n')print('\n'.join(result_split[:max_lines_output]))print("\t ...")print('\n'.join(result_split[-5:]))else:print(result)terminal("ls")
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translated__pycache__ssh.ipynbtest.htmltest.ipynb
Commands can usually receive options (flags
), which are introduced with the -
character. For example, let's look at ls -l
, which returns the list of files in the directory we are in, but with more information.
terminal('ls -l')
total 4512-rw-rw-r-- 1 wallabot wallabot 285898 nov 12 02:07 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 78450 nov 13 00:10 2021-04-23-Calculo-matricial-con-Numpy.ipynb-rw-rw-r-- 1 wallabot wallabot 484213 nov 13 00:44 2021-06-15-Manejo-de-datos-con-Pandas.ipynb-rw-rw-r-- 1 wallabot wallabot 320810 dic 6 00:11 2022-09-12 Introduccion a la terminal.ipynb-rw-rw-r-- 1 wallabot wallabot 320594 dic 6 00:04 2022-09-12 Introduccion a la terminal.txt-rw-rw-r-- 1 wallabot wallabot 119471 oct 3 16:13 command-line-cheat-sheet.pdf-rw-rw-r-- 1 wallabot wallabot 2660 sep 18 03:32 CSS.ipynb-rw-rw-r-- 1 wallabot wallabot 699225 nov 27 04:16 Docker.html-rw-rw-r-- 1 wallabot wallabot 509125 sep 22 16:48 Docker.ipynb-rw-rw-r-- 1 wallabot wallabot 156193 nov 27 04:21 Expresiones regulares.html-rw-rw-r-- 1 wallabot wallabot 53094 oct 2 04:57 Expresiones regulares.ipynbdrwxrwxr-x 2 wallabot wallabot 4096 nov 28 14:39 html_files-rw-rw-r-- 1 wallabot wallabot 14775 sep 18 03:29 html.ipynbdrwxrwxr-x 3 wallabot wallabot 4096 nov 12 01:51 introduccion_python-rw-rw-r-- 1 wallabot wallabot 446172 oct 2 04:39 movies.csv-rw-rw-r-- 1 wallabot wallabot 522197 oct 2 04:33 movies.datdrwxrwxr-x 2 wallabot wallabot 4096 nov 28 14:39 notebooks_translateddrwxrwxr-x 2 wallabot wallabot 4096 ago 27 03:25 __pycache__-rw-rw-r-- 1 wallabot wallabot 586 dic 4 02:31 ssh.ipynb-rw-rw-r-- 1 wallabot wallabot 292936 nov 9 01:46 test.html-rw-rw-r-- 1 wallabot wallabot 260227 nov 9 01:13 test.ipynb
As we can see, we have how many bytes each file occupies, but when we have files that take up a lot of space, this is not very easy to read, so we can add the h
(human
) option that gives us easier to read information.
terminal('ls -lh')
total 4,5M-rw-rw-r-- 1 wallabot wallabot 280K nov 12 02:07 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 77K nov 13 00:10 2021-04-23-Calculo-matricial-con-Numpy.ipynb-rw-rw-r-- 1 wallabot wallabot 473K nov 13 00:44 2021-06-15-Manejo-de-datos-con-Pandas.ipynb-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:11 2022-09-12 Introduccion a la terminal.ipynb-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:04 2022-09-12 Introduccion a la terminal.txt-rw-rw-r-- 1 wallabot wallabot 117K oct 3 16:13 command-line-cheat-sheet.pdf-rw-rw-r-- 1 wallabot wallabot 2,6K sep 18 03:32 CSS.ipynb-rw-rw-r-- 1 wallabot wallabot 683K nov 27 04:16 Docker.html-rw-rw-r-- 1 wallabot wallabot 498K sep 22 16:48 Docker.ipynb-rw-rw-r-- 1 wallabot wallabot 153K nov 27 04:21 Expresiones regulares.html-rw-rw-r-- 1 wallabot wallabot 52K oct 2 04:57 Expresiones regulares.ipynbdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 html_files-rw-rw-r-- 1 wallabot wallabot 15K sep 18 03:29 html.ipynbdrwxrwxr-x 3 wallabot wallabot 4,0K nov 12 01:51 introduccion_python-rw-rw-r-- 1 wallabot wallabot 436K oct 2 04:39 movies.csv-rw-rw-r-- 1 wallabot wallabot 510K oct 2 04:33 movies.datdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 notebooks_translateddrwxrwxr-x 2 wallabot wallabot 4,0K ago 27 03:25 __pycache__-rw-rw-r-- 1 wallabot wallabot 586 dic 4 02:31 ssh.ipynb-rw-rw-r-- 1 wallabot wallabot 287K nov 9 01:46 test.html-rw-rw-r-- 1 wallabot wallabot 255K nov 9 01:13 test.ipynb
If we want to see hidden files, we can use the a
option, which will show us all the files in a directory
terminal('ls -lha')
total 4,5Mdrwxrwxr-x 6 wallabot wallabot 4,0K dic 6 00:04 .drwxrwxr-x 5 wallabot wallabot 4,0K oct 2 03:10 ..-rw-rw-r-- 1 wallabot wallabot 280K nov 12 02:07 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 77K nov 13 00:10 2021-04-23-Calculo-matricial-con-Numpy.ipynb-rw-rw-r-- 1 wallabot wallabot 473K nov 13 00:44 2021-06-15-Manejo-de-datos-con-Pandas.ipynb-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:11 2022-09-12 Introduccion a la terminal.ipynb-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:04 2022-09-12 Introduccion a la terminal.txt-rw-rw-r-- 1 wallabot wallabot 117K oct 3 16:13 command-line-cheat-sheet.pdf-rw-rw-r-- 1 wallabot wallabot 2,6K sep 18 03:32 CSS.ipynb-rw-rw-r-- 1 wallabot wallabot 683K nov 27 04:16 Docker.html-rw-rw-r-- 1 wallabot wallabot 498K sep 22 16:48 Docker.ipynb-rw-rw-r-- 1 wallabot wallabot 153K nov 27 04:21 Expresiones regulares.html-rw-rw-r-- 1 wallabot wallabot 52K oct 2 04:57 Expresiones regulares.ipynbdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 html_files-rw-rw-r-- 1 wallabot wallabot 15K sep 18 03:29 html.ipynbdrwxrwxr-x 3 wallabot wallabot 4,0K nov 12 01:51 introduccion_python-rw-rw-r-- 1 wallabot wallabot 436K oct 2 04:39 movies.csv-rw-rw-r-- 1 wallabot wallabot 510K oct 2 04:33 movies.datdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 notebooks_translateddrwxrwxr-x 2 wallabot wallabot 4,0K ago 27 03:25 __pycache__-rw-rw-r-- 1 wallabot wallabot 586 dic 4 02:31 ssh.ipynb-rw-rw-r-- 1 wallabot wallabot 287K nov 9 01:46 test.html-rw-rw-r-- 1 wallabot wallabot 255K nov 9 01:13 test.ipynb
If what we want is for it to sort them by size, we can use the S
option
terminal('ls -lhS')
total 4,5M-rw-rw-r-- 1 wallabot wallabot 683K nov 27 04:16 Docker.html-rw-rw-r-- 1 wallabot wallabot 510K oct 2 04:33 movies.dat-rw-rw-r-- 1 wallabot wallabot 498K sep 22 16:48 Docker.ipynb-rw-rw-r-- 1 wallabot wallabot 473K nov 13 00:44 2021-06-15-Manejo-de-datos-con-Pandas.ipynb-rw-rw-r-- 1 wallabot wallabot 436K oct 2 04:39 movies.csv-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:11 2022-09-12 Introduccion a la terminal.ipynb-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:04 2022-09-12 Introduccion a la terminal.txt-rw-rw-r-- 1 wallabot wallabot 287K nov 9 01:46 test.html-rw-rw-r-- 1 wallabot wallabot 280K nov 12 02:07 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 255K nov 9 01:13 test.ipynb-rw-rw-r-- 1 wallabot wallabot 153K nov 27 04:21 Expresiones regulares.html-rw-rw-r-- 1 wallabot wallabot 117K oct 3 16:13 command-line-cheat-sheet.pdf-rw-rw-r-- 1 wallabot wallabot 77K nov 13 00:10 2021-04-23-Calculo-matricial-con-Numpy.ipynb-rw-rw-r-- 1 wallabot wallabot 52K oct 2 04:57 Expresiones regulares.ipynb-rw-rw-r-- 1 wallabot wallabot 15K sep 18 03:29 html.ipynbdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 html_filesdrwxrwxr-x 3 wallabot wallabot 4,0K nov 12 01:51 introduccion_pythondrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 notebooks_translateddrwxrwxr-x 2 wallabot wallabot 4,0K ago 27 03:25 __pycache__-rw-rw-r-- 1 wallabot wallabot 2,6K sep 18 03:32 CSS.ipynb-rw-rw-r-- 1 wallabot wallabot 586 dic 4 02:31 ssh.ipynb
If we want it to show us the files sorted alphabetically but in reverse order, we must use the -r
option
terminal('ls -lhr')
total 4,5M-rw-rw-r-- 1 wallabot wallabot 255K nov 9 01:13 test.ipynb-rw-rw-r-- 1 wallabot wallabot 287K nov 9 01:46 test.html-rw-rw-r-- 1 wallabot wallabot 586 dic 4 02:31 ssh.ipynbdrwxrwxr-x 2 wallabot wallabot 4,0K ago 27 03:25 __pycache__drwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 notebooks_translated-rw-rw-r-- 1 wallabot wallabot 510K oct 2 04:33 movies.dat-rw-rw-r-- 1 wallabot wallabot 436K oct 2 04:39 movies.csvdrwxrwxr-x 3 wallabot wallabot 4,0K nov 12 01:51 introduccion_python-rw-rw-r-- 1 wallabot wallabot 15K sep 18 03:29 html.ipynbdrwxrwxr-x 2 wallabot wallabot 4,0K nov 28 14:39 html_files-rw-rw-r-- 1 wallabot wallabot 52K oct 2 04:57 Expresiones regulares.ipynb-rw-rw-r-- 1 wallabot wallabot 153K nov 27 04:21 Expresiones regulares.html-rw-rw-r-- 1 wallabot wallabot 498K sep 22 16:48 Docker.ipynb-rw-rw-r-- 1 wallabot wallabot 683K nov 27 04:16 Docker.html-rw-rw-r-- 1 wallabot wallabot 2,6K sep 18 03:32 CSS.ipynb-rw-rw-r-- 1 wallabot wallabot 117K oct 3 16:13 command-line-cheat-sheet.pdf-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:04 2022-09-12 Introduccion a la terminal.txt-rw-rw-r-- 1 wallabot wallabot 314K dic 6 00:11 2022-09-12 Introduccion a la terminal.ipynb-rw-rw-r-- 1 wallabot wallabot 473K nov 13 00:44 2021-06-15-Manejo-de-datos-con-Pandas.ipynb-rw-rw-r-- 1 wallabot wallabot 77K nov 13 00:10 2021-04-23-Calculo-matricial-con-Numpy.ipynb-rw-rw-r-- 1 wallabot wallabot 280K nov 12 02:07 2021-02-11-Introduccion-a-Python.ipynb
cd
(change directory)
The second command will be cd
(change directory) which allows us to change directories
terminal('cd /home/wallabot/Documentos/')
If we now use ls
again to see the files we have, we see that they change
terminal('cd /home/wallabot/Documentos/')terminal('ls')
aprendiendo-git.pdfbalena-etcher-electron-1.7.9-linux-x64camerasIPDocumentaciongstreamergstreamer_oldjetsonNanokaggleLibrosnerfprueba.txtpytorchwallabotweb
If instead of giving cd
the directory we want to move to, we give it the character -
, it will return to the previous directory where it was.
terminal('cd -')
terminal('cd -')terminal('ls')
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translated__pycache__ssh.ipynbtest.htmltest.ipynb
If we wanted to move to the home
, just entering cd
in the terminal will take us there.
terminal('cd')
pwd
(print working directory)
To obtain the directory we are in, we can use pwd
(print working directory)
terminal('cd')terminal('pwd')
/home/wallabot
We can move using the cd
command via relative paths and absolute paths. For example, let's move to a directory using an absolute path.
terminal('cd /home/wallabot/Documentos/')
terminal('cd /home/wallabot/Documentos/')terminal('pwd')
/home/wallabot/Documentos
terminal('ls')
aprendiendo-git.pdfbalena-etcher-electron-1.7.9-linux-x64camerasIPDocumentaciongstreamergstreamer_oldjetsonNanokaggleLibrosnerfprueba.txtpytorchwallabotweb
We can move using relative paths if we only provide the address from the point where we are located.
terminal('cd web')
terminal('cd web')terminal('pwd')
/home/wallabot/Documentos/web
We can also go up one directory using relative paths with ..
terminal('cd ..')
terminal('cd ..')terminal('pwd')
/home/wallabot/Documentos
If instead of ..
we use .
, we are referring to the directory we are currently in. That is, if we use cd .
, we will not move because we are telling the terminal to go to the directory we are in.
terminal('cd .')
terminal('cd .')terminal('pwd')
/home/wallabot/Documentos
Let's move to a path where we have files to display the following command
terminal('cd web/portafolio/posts/')
terminal('cd web/portafolio/posts/')terminal('ls')
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translated__pycache__ssh.ipynbtest.htmltest.ipynb
Information of files with file
If I don't know what type of file a particular one is, I can get a description using the file
command
terminal('file 2021-02-11-Introduccion-a-Python.ipynb')
2021-02-11-Introduccion-a-Python.ipynb: UTF-8 Unicode text, with very long lines
Manipulating files and directories
Let's move first to the home.
terminal('cd /home/wallabot/Documentos/')
Directory Tree with tree
We can see the entire structure of the folder we are in using the tree
command.
terminal('cd /home/wallabot/Documentos/')terminal('tree', max_lines_output=20)
.├── aprendiendo-git.pdf├── balena-etcher-electron-1.7.9-linux-x64│ └── balenaEtcher-1.7.9-x64.AppImage├── camerasIP│ ├── camerasIP.py│ ├── camerasIP.sh│ ├── config.py│ ├── __pycache__│ │ ├── config.cpython-38.pyc│ │ └── config.cpython-39.pyc│ └── README.md├── Documentacion│ ├── Curriculum Vitae (5).pdf│ ├── Firma Pris.PNG│ └── Firma.png├── gstreamer│ ├── basic_tutorial_c│ │ ├── basic_tutorial_1_hello_world│ │ │ ├── basic-tutorial-1...├── upload_page.py└── utils.py873 directories, 119679 files
But at the output, we have too many lines, and this is because tree
is a command that shows all the files from the path we are in, making it a bit hard to read. However, with the L
option, we can specify how many levels we want it to go deep.
terminal('tree -L 2')
.├── aprendiendo-git.pdf├── balena-etcher-electron-1.7.9-linux-x64│ └── balenaEtcher-1.7.9-x64.AppImage├── camerasIP│ ├── camerasIP.py│ ├── camerasIP.sh│ ├── config.py│ ├── __pycache__│ └── README.md├── Documentacion│ ├── Curriculum Vitae (5).pdf│ ├── Firma Pris.PNG│ └── Firma.png├── gstreamer│ ├── basic_tutorial_c│ └── README.md├── gstreamer_old│ ├── basic_tutorial_c│ └── basic_tutorial_python├── jetsonNano│ ├── apuntes-Jetson-Nano│ ├── deepstream_apps│ ├── deepstream_nano│ └── Digital zoom├── kaggle│ └── hubmap├── Libros│ └── aprendiendo-git.pdf├── nerf│ └── instant-ngp├── prueba.txt├── pytorch│ └── Curso_Pytorch├── wallabot│ ├── Microfono - Blue Yeti X│ ├── placa base - Asus prime x570-p│ └── Silla - Corsair T3 Rush└── web├── jupyter-to-html├── jupyter-translator├── portafolio└── wordpress_api_rest30 directories, 12 files
We can see that it shows there are 30 directories and 12 files, whereas previously it indicated 873 directories, 119679 files.
Create directories with mkdir
(make directory)
If we want to create a new directory, we can use the mkdir
(make directory) command followed by a name
terminal("cd /home/wallabot/Documentos/web/portafolio/posts/")
terminal("cd /home/wallabot/Documentos/web/portafolio/posts/")terminal('mkdir prueba')
terminal('ls')
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedprueba__pycache__ssh.ipynbtest.htmltest.ipynb
If we want to create a directory with spaces in the name, we need to put the name in quotes.
terminal('mkdir "directorio prueba"')
terminal('ls')
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbdirectorio pruebaDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedprueba__pycache__ssh.ipynbtest.htmltest.ipynb
Let's go inside the prueba
folder that we have created, to continue viewing the terminal there.
terminal("cd prueba")
Create files with touch
In case we want to create a file, the command we have to use is touch
terminal("cd prueba")terminal("touch prueba.txt")
terminal("ls")
prueba.txt
Copy Files with cp
(copy)
If we want to copy a file, we do it using the cp
(copy) command
terminal("cp prueba.txt prueba_copy.txt")
terminal("ls")
prueba_copy.txtprueba.txt
Move files with mv
(move)
If what we want is to move it, what we use is the mv
(move) command
terminal("mv prueba.txt ../prueba.txt")
terminal("ls")
prueba_copy.txt
terminal("ls ../")
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbdirectorio pruebaDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedpruebaprueba.txt__pycache__ssh.ipynbtest.htmltest.ipynb
Rename files with mv
(move)
The mv
command also helps us rename files, as moving them within the same directory but giving them a different name ultimately renames the file.
terminal("mv prueba_copy.txt prueba_move.txt")
terminal("ls")
prueba_move.txt
Delete files with rm
(remove)
To delete files or directories we use the rm
(remove) command
terminal("rm prueba_move.txt")
terminal("ls")
Delete directories with rm -r
(remove recursive)
If what we want is to delete a directory with files inside, we must use the -r
flag.
terminal("cd ..")
terminal("cd ..")terminal('rm -r "directorio prueba"')
terminal("ls")
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedpruebaprueba.txt__pycache__ssh.ipynbtest.htmltest.ipynb
As you can see it never asks if we are sure, to make it ask we need to add the flag -i
(interactive
)
terminal("rm -i prueba.txt")
rm: ¿borrar el fichero regular vacío 'prueba.txt'? (s/n) s
Synchronize files using rsync
So far we have seen how to copy, move, and delete files, but let's suppose we have a folder and we copy those files to another one. Now let's suppose we modify a file in the first folder and we want the second one to have the changes. We have two options, copy all the files again, or synchronize them using rsync
First, let's create a new folder in which we will create several files
!mkdir sourcefolder!touch sourcefolder/file1 sourcefolder/file2 sourcefolder/file3
Now we create a second folder which is the one we are going to synchronize with the first one.
!mkdir sourcefolder!touch sourcefolder/file1 sourcefolder/file2 sourcefolder/file3!mkdir syncfolder
!mkdir sourcefolder!touch sourcefolder/file1 sourcefolder/file2 sourcefolder/file3!mkdir syncfolder!echo "ls sourcefolder:" && ls sourcefolder && echo "ls syncfolder:" && ls syncfolder
ls sourcefolder:file1 file2 file3ls syncfolder:
We synchronize the two folders with rsync
, the first time it will only copy the files from the first folder to the second. To do this, we must also add the -r
(recursive) flag.
!rsync -r sourcefolder/ syncfolder/
!rsync -r sourcefolder/ syncfolder/!echo "ls sourcefolder:" && ls sourcefolder && echo "ls syncfolder:" && ls syncfolder
ls sourcefolder:file1 file2 file3ls syncfolder:file1 file2 file3
If I now create a new file in sourcefolder
and sync again, only that file will be copied to syncfolder
. To see that only one file is copied, we can use the -v
(verbose) flag.
!touch sourcefolder/file4
!touch sourcefolder/file4!rsync -r -v sourcefolder/ syncfolder/
sending incremental file listfile1file2file3file4sent 269 bytes received 92 bytes 722.00 bytes/sectotal size is 0 speedup is 0.00
But it seems that it has copied all the files, so to prevent this from happening and to copy only the ones that have been modified, you need to use the -u
flag.
!touch sourcefolder/file5
!touch sourcefolder/file5!rsync -r -v -u sourcefolder/ syncfolder/
sending incremental file listfile5sent 165 bytes received 35 bytes 400.00 bytes/sectotal size is 0 speedup is 0.00
!echo "ls sourcefolder:" && ls sourcefolder && echo "ls syncfolder:" && ls syncfolder
ls sourcefolder:file1 file2 file3 file4 file5ls syncfolder:file1 file2 file3 file4 file5
What happens if I create a new file in syncfolder
?
!touch syncfolder/file6
!touch syncfolder/file6!rsync -r -v -u sourcefolder/ syncfolder/
sending incremental file listsent 122 bytes received 12 bytes 268.00 bytes/sectotal size is 0 speedup is 0.00
!echo "ls sourcefolder:" && ls sourcefolder && echo "ls syncfolder:" && ls syncfolder
ls sourcefolder:file1 file2 file3 file4 file5ls syncfolder:file1 file2 file3 file4 file5 file6
It doesn't sync it, so it's important to keep this in mind
Some important flags to keep in mind are:
-a
: This flag is a shortcut for several options, including-r
(recursive),-l
(copy symbolic links),-p
(preserve permissions),-t
(preserve modification time), and-g
(preserve group). This option is useful for making an exact copy of a directory, including all its subdirectories and files.*-v
: This flag activates verbose output, which shows the files being copied and the progress of the operation. *-r
: This flag is used to copy recursively, which means it copies all subfolders and files within a directory. *-u
: This flag is used to copy only the new or modified files. If a file already exists at the destination and is more recent than the source file, it is not copied. *-n
: This flag is used to perform a dry run, which means no changes are made to the destination. *--exclude
: This flag is used to exclude specific files or folders from the copy operation. You can specify multiple files or folders to exclude by using this option multiple times. *-z
: This flag is used to compress the data during transfer, which reduces the bandwidth used and speeds up the transfer rate.*-h
: this flag is used to display information in a more readable format, especially when working with large amounts of data or large file sizes.
We delete the two created folders
!rm -r sourcefolder syncfolder
Exploring the Content of the Files
To avoid having to open a file from a graphical interface, we have several ways. First, I am going to copy a text file into this folder.
!rm -r sourcefolder syncfolderterminal("cd prueba")
!rm -r sourcefolder syncfolderterminal("cd prueba")terminal("cp ../2021-02-11-Introduccion-a-Python.ipynb .")
terminal("ls")
2021-02-11-Introduccion-a-Python.ipynb
File Header with head
The first command to be able to look inside a text file is head
, which allows us to see the first 10 lines of a file, but if you use the -n
flag you can indicate the number of lines.
terminal("head 2021-02-11-Introduccion-a-Python.ipynb")
{"cells": [{"cell_type": "markdown","metadata": {"id": "dsaKCKL0IxZl"},"source": ["# Introducción a Python"]
terminal("head -n 5 2021-02-11-Introduccion-a-Python.ipynb")
{"cells": [{"cell_type": "markdown","metadata": {
End of a file with tail
In case you want to see the last lines, we use tail
terminal("tail 2021-02-11-Introduccion-a-Python.ipynb")
},"vscode": {"interpreter": {"hash": "d5745ab6aba164e1152437c779991855725055592b9f2bdb41a4825db7168d26"}}},"nbformat": 4,"nbformat_minor": 0}
terminal("tail -n 5 2021-02-11-Introduccion-a-Python.ipynb")
}},"nbformat": 4,"nbformat_minor": 0}
If we want to continuously see the latest lines of a file, for example, we want to continuously monitor a LOG file to see events, we add the -f
flag, this will make the terminal continuously check the file, and each time a new line appears in it, it will display it.
For example, if I monitor the login log on my machine
!tail -f /var/log/auth.log
Dec 1 16:27:22 wallabot gcr-prompter[1457]: Gcr: calling the PromptDone method on /org/gnome/keyring/Prompt/p2@:1.26, and ignoring replyDec 1 16:27:22 wallabot gnome-keyring-daemon[1178]: asked to register item /org/freedesktop/secrets/collection/login/10, but it's already registeredDec 1 16:27:26 wallabot systemd-logind[835]: Watching system buttons on /dev/input/event28 (Logitech Wireless Mouse MX Master 3)Dec 1 16:27:33 wallabot gcr-prompter[1457]: Gcr: 10 second inactivity timeout, quittingDec 1 16:27:33 wallabot gcr-prompter[1457]: Gcr: unregistering prompterDec 1 16:27:33 wallabot gcr-prompter[1457]: Gcr: disposing prompterDec 1 16:27:33 wallabot gcr-prompter[1457]: Gcr: finalizing prompterDec 1 16:27:34 wallabot polkitd(authority=local): Operator of unix-session:1 successfully authenticated as unix-user:wallabot to gain TEMPORARY authorization for action org.debian.apt.install-or-remove-packages for system-bus-name::1.96 [/usr/bin/python3 /usr/bin/update-manager --no-update --no-focus-on-map] (owned by unix-user:wallabot)Dec 1 16:27:42 wallabot systemd-logind[835]: Watching system buttons on /dev/input/event30 (T9-R (AVRCP))Dec 1 16:27:49 wallabot gnome-keyring-daemon[1178]: asked to register item /org/freedesktop/secrets/collection/login/2, but it's already registered
We see in the last two lines my login when I turned on my computer today.
Now I connect to my own machine via SSH
!ssh localhost
wallabot@localhost's password:Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.15.0-53-generic x86_64)* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/advantage1 device has a firmware upgrade available.Run `fwupdmgr get-upgrades` for more information.Se pueden aplicar 0 actualizaciones de forma inmediata.Your Hardware Enablement Stack (HWE) is supported until April 2025.*** System restart required ***Last login: Sun May 8 02:18:09 2022 from 192.168.1.147
In the console where I was monitoring the login, two new lines have appeared
Dec 1 16:32:23 wallabot sshd[25647]: Accepted password for wallabot from 127.0.0.1 port 54668 ssh2Dec 1 16:32:23 wallabot sshd[25647]: pam_unix(sshd:session): session opened for user wallabot by (uid=0)Dec 1 16:32:23 wallabot systemd-logind[835]: New session 4 of user wallabot.
And when I close the SSH session, two new lines appear.
Dec 1 16:33:52 wallabot sshd[25647]: pam_unix(sshd:session): session closed for user wallabotDec 1 16:33:52 wallabot systemd-logind[835]: Session 4 logged out. Waiting for processes to exit.Dec 1 16:33:52 wallabot systemd-logind[835]: Removed session 4.
The most powerful file viewer: less
One of the most powerful commands to view inside files is less
terminal("less 2021-02-11-Introduccion-a-Python.ipynb", max_lines_output=20)
{"cells": [{"cell_type": "markdown","metadata": {"id": "dsaKCKL0IxZl"},"source": ["# Introducción a Python"]},{"cell_type": "markdown","metadata": {"id": "Ho_8zgIiI0We"},"source": ["## 1. Resumen"]},...},"nbformat": 4,"nbformat_minor": 0}
Being inside a notebook, you can't really see what happens when using less
, but when we use it, we enter the document, and we can navigate through it using the keyboard or the mouse.
If we want to search for something within the document, we write the character /
and what we want to search for. To switch between the different instances it has found, we press the n
key, and if we want to go back in the searches, we press shift+n
.
To exit, just press q
The cat
viewer
It does not allow you to browse the file or perform searches.
terminal("cat 2021-02-11-Introduccion-a-Python.ipynb", max_lines_output=20)
{"cells": [{"cell_type": "markdown","metadata": {"id": "dsaKCKL0IxZl"},"source": ["# Introducción a Python"]},{"cell_type": "markdown","metadata": {"id": "Ho_8zgIiI0We"},"source": ["## 1. Resumen"]},...},"nbformat": 4,"nbformat_minor": 0}
Default system editor xdg-open
If we want to open it with the file's default editor, we need to use xdg-open
terminal("xdg-open 2021-02-11-Introducción-a-Python.ipynb")
File manager nautilus
If what we want is to open the folder we are in, we use nautilus
terminal("nautilus")
And if what we want is for it to open in a specific path, the path is included.
terminal("nautilus ~/")
Word count of a file with wc
(word count)
Finally, a very useful command is wc
(word count), which shows you how many lines, words, and bytes a file has
terminal("wc 2021-02-11-Introduccion-a-Python.ipynb")
11678 25703 285898 2021-02-11-Introduccion-a-Python.ipynb
As we can see, the file has 11678 lines, 25703 words, and occupies 285898 bytes.
What is a command
A command can be four things* An executable program, these are usually stored in the /usr/bin
path* A shell command * A shell function * An alias
To see which class a command belongs to, we use type
!type cd
cd is a shell builtin
!type mkdir
mkdir is /usr/bin/mkdir
!type ls
ls is /usr/bin/ls
What is an alias?
An alias is a command that we define ourselves, it is defined using the alias
command. For example, we are going to create the alias l
that will perform ls -h
!alias l='ls -l'
When we execute l
, it shows us the result of ls -h
.
!alias l='ls -l'!l
2021-02-11-Introducción-a-Python.ipynb
But this has the problem that when we close the terminal, the alias
disappears. Later we will learn how to create permanent alias
.
Command Help
Help with help
With many shell commands, we can get their help using the help
command
!help cd
cd: cd [-L|[-P [-e]]] [dir]Modifica el directorio de trabajo del shell.Modifica el directorio actual a DIR. DIR por defecto es el valor de lavariable de shell HOME.La variable CDPATH define la ruta de búsqueda para el directorio quecontiene DIR. Los nombres alternativos de directorio en CDPATH seseparan con dos puntos (:). Un nombre de directorio nulo es igual queel directorio actual. Si DIR comienza con una barra inclinada (/),entonces no se usa CDPATH.Si no se encuentra el directorio, y la opción del shell "cdable_vars"está activa, entonces se trata la palabra como un nombre de variable.Si esa variable tiene un valor, se utiliza su valor para DIR.Opciones:-L fuerza a seguir los enlaces simbólicos: resuelve los enlacessimbólicos en DIR después de procesar las instancias de ".."-P usa la estructura física de directorios sin seguir los enlacessimbólicos: resuelve los enlaces simbólicos en DIR antes de procesarlas instancias de ".."-e si se da la opción -P y el directorio actual de trabajo no sepuede determinar con éxito, termina con un estado diferente de cero.La acción por defecto es seguir los enlaces simbólicos, como si seespecificara "-L".".." se procesa quitando la componente del nombre de la ruta inmediatamenteanterior hasta una barra inclinada o el comienzo de DIR.Estado de Salida:Devuelve 0 si se cambia el directorio, y si $PWD está definido comocorrecto cuando se emplee -P; de otra forma es diferente a cero.
Manual with man
Another command is man
, which refers to the user manual.
terminal("man ls", max_lines_output=20)
LS(1) User Commands LS(1)NAMEls - list directory contentsSYNOPSISls [OPTION]... [FILE]...DESCRIPTIONList information about the FILEs (the current directory by default).Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐fied.Mandatory arguments to long options are mandatory for short optionstoo.-a, --alldo not ignore entries starting with .-A, --almost-all...Full documentation at: <https://www.gnu.org/software/coreutils/ls>or available locally via: info '(coreutils) ls invocation'GNU coreutils 8.30 September 2019 LS(1)
To exit, press q
, as man
uses less
as the manual viewer
Information with info
Another command is info
terminal("info ls", max_lines_output=20)
File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directory listing10.1 ‘ls’: List directory contents==================================The ‘ls’ program lists information about files (of any type, includingdirectories). Options and file arguments can be intermixed arbitrarily,as usual.For non-option command-line arguments that are directories, bydefault ‘ls’ lists the contents of directories, not recursively, andomitting files with names beginning with ‘.’. For other non-optionarguments, by default ‘ls’ lists just the file name. If no non-optionargument is specified, ‘ls’ operates on the current directory, acting asif it had been invoked with a single argument of ‘.’.By default, the output is sorted alphabetically, according to thelocale settings in effect.(1) If standard output is a terminal, theoutput is in columns (sorted vertically) and control characters areoutput as question marks; otherwise, the output is listed one per line...‘--show-control-chars’Print nongraphic characters as-is in file names. This is thedefault unless the output is a terminal and the program is ‘ls’.
To exit, press q
, as info
uses less
as the information viewer.
Information about a command with whatis
Another command is whatis
terminal("whatis ls")
ls (1) - list directory contents
Wildcards
Wildcards are special characters that help us perform special searches. For example, if I want to search for all files ending in .txt
. Let's create a few files to see them.
terminal("touch file.txt dot.txt dot2.txt index.html datos1 datos123 Abc")
terminal("ls")
2021-02-11-Introduccion-a-Python.ipynbAbcdatos1datos123dot2.txtdot.txtfile.txtindex.html
All characters *
Let's now search for all .txt
files
!ls *.txt
dot2.txt dot.txt file.txt
Let's now search for all that start with the word datos
!ls datos*
datos1 datos123
Numbers ?
But what if we actually want to show all the files that start with the word datos
but are followed only by a number? We need to use a question mark ?
.
!ls datos?
datos1
If what we want is for it to have three numbers, then we have to put three question marks ???
!ls datos???
datos123
Uppercase [[:upper:]]
If we want it to search for files that start with uppercase letters
!ls [[:upper:]]*
Abc
Lowercase [[:lower:]]
For files that start with lowercase.
!ls [[:lower:]]*
datos1 datos123 dot2.txt dot.txt file.txt index.html
Classes
By using brackets, we can create classes; thus, if we want to search for files that start with the letters d
or f
followed by any character
!ls [df]*
datos1 datos123 dot2.txt dot.txt file.txt
Redirections: how the shell works
A command works as follows
It has a standard input
, which by default is the text we enter through the keyboard, a standard output
, which by default is the text that comes out on the console, and a standard error
which is also by default a text that comes out on the console, but has a different format.
Redirecting standard output
But with the >
character, we can modify the standard output
of a command. For example, if we want to list the files in the current directory with ls
, but we don't want the result to be printed on the screen, instead, we want it to be saved in a file, we would do the following ls > lista.txt
, this writes the list to lista.txt
, and if lista.txt
does not exist, it creates it.
!ls > lista.txt
We see that the file has been created and we see what is inside.
!ls > lista.txtterminal("ls")
2021-02-11-Introduccion-a-Python.ipynbAbcdatos1datos123dot2.txtdot.txtfile.txtindex.htmllista.txt
terminal("cat lista.txt")
2021-02-11-Introduccion-a-Python.ipynbAbcdatos1datos123dot2.txtdot.txtfile.txtindex.htmllista.txt
We see that lista.txt
appears within lista.txt
, that's because it first creates the file and then executes the command.
We do the same, but with the parent folder
!ls ../ > lista.txt
If we look inside lista.txt
again
!ls ../ > lista.txtterminal("cat lista.txt")
2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedprueba__pycache__ssh.ipynbtest.htmltest.ipynb
We see that the content is overwritten
If what we want is to concatenate the content, we should use >>
!ls > lista.txt
!ls > lista.txt!ls ../ >> lista.txt
!ls > lista.txt!ls ../ >> lista.txtterminal("cat lista.txt")
2021-02-11-Introduccion-a-Python.ipynbAbcdatos1datos123dot2.txtdot.txtfile.txtindex.htmllista.txt2021-02-11-Introduccion-a-Python.ipynb2021-04-23-Calculo-matricial-con-Numpy.ipynb2021-06-15-Manejo-de-datos-con-Pandas.ipynb2022-09-12 Introduccion a la terminal.ipynb2022-09-12 Introduccion a la terminal.txtcommand-line-cheat-sheet.pdfCSS.ipynbDocker.htmlDocker.ipynbExpresiones regulares.htmlExpresiones regulares.ipynbhtml_fileshtml.ipynbintroduccion_pythonmovies.csvmovies.datnotebooks_translatedprueba__pycache__ssh.ipynbtest.htmltest.ipynb
Now the information has been concatenated
This is very useful for creating log files
Redirecting standard error
If we perform an incorrect operation, we get an error. Let's see what happens when we redirect a command that gives an error.
!ls fjhdsalkfs > lista.txt
ls: no se puede acceder a 'fjhdsalkfs': No existe el archivo o el directorio
As we can see, it has given an error, but if we now look inside lista.txt
terminal("cat lista.txt")
We see that the file is empty, that's because we haven't redirected the standard error
to lista.txt
, but the standard output
. As we have seen in the image, there are two standard outputs in a command, the first one is the standard output
and the second one is the standard error
, so to redirect the standard error you have to indicate it with 2>
. Let's proceed this way now.
!ls kjhsfskjd 2> lista.txt
!ls kjhsfskjd 2> lista.txtterminal("cat lista.txt")
ls: no se puede acceder a 'kjhsfskjd': No existe el archivo o el directorio
As we can see, it has now been redirected
Redirecting standard output
and standard error
If we want to redirect both, we use the following
!ls kjhsfskjd > lista.txt 2>&1
Let's look inside lista.txt
!ls kjhsfskjd > lista.txt 2>&1terminal("cat lista.txt")
ls: no se puede acceder a 'kjhsfskjd': No existe el archivo o el directorio
If we now execute a command without errors
!ls . >> lista.txt 2>&1
Let's look inside lista.txt
(note, now we have concatenated)
!ls . >> lista.txt 2>&1terminal("cat lista.txt")
ls: no se puede acceder a 'kjhsfskjd': No existe el archivo o el directorio2021-02-11-Introduccion-a-Python.ipynbAbcdatos1datos123dot2.txtdot.txtfile.txtindex.htmllista.txt
As you can see, both standard error
and standard output
have been redirected to the same file.
Pipelines
We can create pipelines by making the standard output
of one command become the standard input
of another. For example, let's make the output of ls -lha
the input of grep
, which we will see later, but it is a command for searching.
!ls -lha | grep -i "txt"
-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt
As we can see, what we have done is pipe the output of ls
to grep
with which we have searched for any file with txt
in the name
Control Operators - Chaining Commands
Commands Sequentially
One way to chain commands sequentially is to separate them using ;
. This creates different threads for each task.
!ls; echo 'Hola'; cal
2021-02-11-Introduccion-a-Python.ipynb datos123 file.txtAbc dot2.txt index.htmldatos1 dot.txt lista.txtHolaDiciembre 2022do lu ma mi ju vi sá1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31
As we can see, the ls
command was executed first, then Hello was printed thanks to the command echo "Hola"
, and finally a calendar was printed thanks to the command cal
.
Let's now do another example to see that they execute sequentially.
!echo "Before touch;"; ls -lha; touch secuential.txt; echo "After touch:"; ls -lha
Before touch;total 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:04 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txtAfter touch:total 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:07 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:07 secuential.txt
As you can see, in the first ls
, secuential.txt
does not appear, while in the second one it does. This means that the commands were executed sequentially, one after the other.
Parallel Commands
If what we want is for the commands to be executed in parallel, we must use the &
operator. This will create a new process for each command.
Let's look at the previous example
!rm secuential.txt
!rm secuential.txt!echo "Before touch;" & ls -lha & touch secuential.txt & echo "After touch:" & ls -lha
Before touch;After touch:total 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:08 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
Now you can see that they have not been executed sequentially, since the echo
s have been executed first, which will take the least time, and then the rest.
Conditional Commands
And
Using the &&
operator, a command will execute when the previous one has successfully run
!rm secuential.txt
!rm secuential.txt!echo "Before touch;" && ls -lha && touch secuential.txt && echo "After touch:" && ls -lha
Before touch;total 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:08 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txtAfter touch:total 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:08 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
Here we can see how it has been executed one after another, that is, a command does not start until the previous one ends
But then, what's the difference between ;
and &&
?
In the first case, the sequential ;
, one command is executed first and then the other, but for a command to be executed, it doesn't matter if the previous one was executed successfully.
!rm prueba ; ls -lha
rm: no se puede borrar 'prueba': No existe el archivo o el directoriototal 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:08 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
As you can see, first rm prueba
is executed, an error occurs, and ls -lha prueba
is still executed.
In the conditional manner &&
, if a command does not execute successfully, the next one will not be executed.
!rm prueba && ls -lha
rm: no se puede borrar 'prueba': No existe el archivo o el directorio
As you can see ls -lha prueba
does not execute because rm prueba
has given an error
Or
Unlike &&
, the 'or' will execute all processes regardless of their result. The ||
operator should be used.
!rm prueba || ls -lha
rm: no se puede borrar 'prueba': No existe el archivo o el directoriototal 292Kdrwxrwxr-x 2 wallabot wallabot 4,0K dic 6 01:08 .drwxrwxr-x 7 wallabot wallabot 4,0K dic 6 00:24 ..-rw-rw-r-- 1 wallabot wallabot 280K dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
The difference between this and ;
is that ||
(or) does not create a new thread for each command
How Permissions are Managed
When listing the files in a directory with the -l
(long) flag, some symbols appear next to each file.
!mkdir subdirectorio
!mkdir subdirectorio!ls -l
total 288-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txtdrwxrwxr-x 2 wallabot wallabot 4096 dic 6 01:10 subdirectorio
This gives us information about each file
First, let's see what types of files there are. * -: Regular file* d: Directory* l: Symbolic link* b: Special block file. These are files that manage data block information, such as a USB
Later we will see the types of mode:
<table border="1">
``````markdown
<header>
```<tr><th scope="col" colspan="3">Owner</th><th scope="col" colspan="3">Group</th><th scope="col" colspan="3">Mundo</th>```markdown
</tr>
``` </header><body> <tr><th scope="row" colspan="3">rwx</th><th scope="row" colspan="3">r-x</th><th scope="row" colspan="3">r-x</th> </tr> <tr> <th scope="row">1</th><th scope="row">1</th> <th scope="row">1</th><th scope="row">1</th><th scope="row">0</th> <th scope="row">1</th> <th scope="row">1</th><th scope="row">0</th><th scope="row">1</th> </tr> <tr> <th scope="row" colspan="3">7</th> <th scope="row" colspan="3">5</th> <th scope="row" colspan="3">5</th> </tr><body></table>
* r: read* w: write * x: execute
Symbolic mode:* u: User-only* g: Group only* o: Only for others (world)* a: For all
Modifying Permissions in the Terminal
We create a new file
terminal("cd subdirectorio")
terminal("cd subdirectorio")!echo "hola mundo" > mitexto.txt
terminal("cd subdirectorio")!echo "hola mundo" > mitexto.txt!cat mitexto.txt
hola mundo
Let's see the permissions it has.
!ls -l
total 4-rw-rw-r-- 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt
As we can see, it has read and write permissions for my user and the group, and only read permissions for the rest (world)
Changing Permissions with chmod
(change mode)
To change the permissions of a file we use the chmod
(change mode) command, where we need to set the user's permissions first in octal, then the group's, and lastly the others'.
!chmod 755 mitexto.txt
!chmod 755 mitexto.txt!ls -l
total 4-rwxr-xr-x 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt
We see that now my user has read, write, and execute permissions, while the group and the rest of the world have read and execute permissions.
We are going to remove the read permissions only for my user. To change only the permissions for a user, we use the symbolic identifier, a +
if we want to add permissions or a -
if we want to remove them or a =
if we want to reset them followed by the type of permission
!chmod u-r mitexto.txt
!chmod u-r mitexto.txt!ls -l
total 4--wxr-xr-x 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt
!cat mitexto.txt
cat: mitexto.txt: Permiso denegado
As we can see, by removing read permissions for my user, we cannot read the file.
We will grant the read permission again
!chmod u+r mitexto.txt
!chmod u+r mitexto.txt!ls -l
total 4-rwxr-xr-x 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt
!cat mitexto.txt
hola mundo
If we want to add or remove permissions for more than one user, we do so by separating each permission with a ,
!chmod u-x,go=w mitexto.txt
!chmod u-x,go=w mitexto.txt!ls -l
total 4-rw--w--w- 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt
As can be seen, execution permission has been removed for the user and write-only permission has been set for the group and the rest of the world.
User Identification with whoami
To find out who we are, we can use the whoami
(who am I) command.
!whoami
wallabot
User Information with id
Another way, which also provides more information, is the id
command.
!id
uid=1000(wallabot) gid=1000(wallabot) grupos=1000(wallabot),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare),998(docker)
This command tells us that our user ID is 1000, the group ID is 1000, and that we belong to the groups wallabot, adm, cdrom, sudo, dip, plugdev, lpadmin, lxd, sambashare, and docker
Change of user with the su
(switch user) command
If we want to switch users, we use the su
(switch user) command. For certain users, we need to use sudo
(superuser do)
!sudo su root
root@wallabot:/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio#
As we can see, the prompt
changes and now indicates that we are the user root
.
Let's go to the home folder
!cd
root@wallabot:~#
But in Linux there is a home folder for each user, we can see this if we run the command pwd
!pwd
/root
I am going to create a file in the folder where I previously created the mitexto.txt file.
!touch /home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio/rootfile.txt
I change back to my user
!su wallabot
wallabot@wallabot:
And I go to the directory where the files I have created are located.
!cd /home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio
We see the files that are there and their permissions
!cd /home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio!ls -l
total 4-rw--w--w- 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt-rw-r--r-- 1 root root 0 dic 6 01:22 rootfile.txt
As we can see, the owner and group of the file rootfile.txt is the user root
If I, now that I am the user wallabot, try to delete the file rootfile.txt
!rm rootfile.txt
rm: ¿borrar el fichero regular vacío 'rootfile.txt' protegido contra escritura? (s/n)
As we can see, it asks us if we want to delete it, as it belongs to another user.
Change a User's Password
If I want to modify the password of the currently active user, I use the passwd
(password) command.
First, I check which user I am
!whoami
wallabot
And now we try to change the password
!passwd
$ passwdCambiando la contraseña de wallabot.Contraseña actual de :Nueva contraseña:Vuelva a escribir la nueva contraseña
As we can see, it asks for the current password in order to change it.
Symbolic Links
We can create symbolic links to a specific path using the ln
(link) command followed by the -s
(symbolic) flag, the directory, and the name of the link.
!ln -s /home/wallabot/Documentos/web web
If we now list the files
!ln -s /home/wallabot/Documentos/web web!ls -l
total 4-rw--w--w- 1 wallabot wallabot 11 dic 6 01:10 mitexto.txt-rw-r--r-- 1 root root 0 dic 6 01:22 rootfile.txtlrwxrwxrwx 1 wallabot wallabot 29 dic 6 01:28 web -> /home/wallabot/Documentos/web
We see the symbolic link web
pointing to /home/wallabot/Documentos/web:
I can now go to web
terminal("cd web")
terminal("cd web")!pwd
/home/wallabot/Documentos/web
Set environment variables
Viewing Environment Variables with printenv
With the printenv
command, we can see all the environment variables
!printenv
GJS_DEBUG_TOPICS=JS ERROR;JS LOGVSCODE_CWD=/home/wallabotLESSOPEN=| /usr/bin/lesspipe %sCONDA_PROMPT_MODIFIER=(base)PYTHONIOENCODING=utf-8USER=wallabotVSCODE_NLS_CONFIG={"locale":"es","availableLanguages":{"*":"es"},"_languagePackId":"b07c40c9acb9e1d7b3ca14b06f814803.es","_translationsConfigFile":"/home/wallabot/.config/Code/clp/b07c40c9acb9e1d7b3ca14b06f814803.es/tcf.json","_cacheRoot":"/home/wallabot/.config/Code/clp/b07c40c9acb9e1d7b3ca14b06f814803.es","_resolvedLanguagePackCoreLocation":"/home/wallabot/.config/Code/clp/b07c40c9acb9e1d7b3ca14b06f814803.es/6261075646f055b99068d3688932416f2346dd3b","_corruptedFile":"/home/wallabot/.config/Code/clp/b07c40c9acb9e1d7b3ca14b06f814803.es/corrupted.info","_languagePackSupport":true}VSCODE_HANDLES_UNCAUGHT_ERRORS=trueMPLBACKEND=module://ipykernel.pylab.backend_inlineSSH_AGENT_PID=1373XDG_SESSION_TYPE=x11SHLVL=0HOME=/home/wallabotCHROME_DESKTOP=code-url-handler.desktopCONDA_SHLVL=1DESKTOP_SESSION=ubuntuGIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/code.desktopVSCODE_IPC_HOOK=/run/user/1000/vscode-26527400-1.73.1-main.sockPYTHONUNBUFFERED=1GTK_MODULES=gail:atk-bridgeGNOME_SHELL_SESSION_MODE=ubuntuAPPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=truePAGER=catMANAGERPID=1153DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/busGIO_LAUNCHED_DESKTOP_FILE_PID=3897_CE_M=IM_CONFIG_PHASE=1LOGNAME=wallabot_=/home/wallabot/anaconda3/bin/pythonJOURNAL_STREAM=8:52662XDG_SESSION_CLASS=userUSERNAME=wallabotTERM=xterm-colorGNOME_DESKTOP_SESSION_ID=this-is-deprecated_CE_CONDA=WINDOWPATH=2PATH=/home/wallabot/anaconda3/bin:/home/wallabot/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/binSESSION_MANAGER=local/wallabot:@/tmp/.ICE-unix/1410,unix/wallabot:/tmp/.ICE-unix/1410INVOCATION_ID=73bba2d15f2e492fa6c16538996a2556VSCODE_AMD_ENTRYPOINT=vs/workbench/api/node/extensionHostProcessXDG_RUNTIME_DIR=/run/user/1000XDG_MENU_PREFIX=gnome-GDK_BACKEND=x11DISPLAY=:0LANG=es_ES.UTF-8XDG_CURRENT_DESKTOP=UnityXAUTHORITY=/run/user/1000/gdm/XauthorityXDG_SESSION_DESKTOP=ubuntuXMODIFIERS=@im=ibusLS_COLORS=SSH_AUTH_SOCK=/run/user/1000/keyring/sshORIGINAL_XDG_CURRENT_DESKTOP=ubuntu:GNOMECONDA_PYTHON_EXE=/home/wallabot/anaconda3/bin/pythonSHELL=/bin/bashELECTRON_RUN_AS_NODE=1QT_ACCESSIBILITY=1GDMSESSION=ubuntuLESSCLOSE=/usr/bin/lesspipe %s %sCONDA_DEFAULT_ENV=basePYDEVD_IPYTHON_COMPATIBLE_DEBUGGING=1GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1GJS_DEBUG_OUTPUT=stderrQT_IM_MODULE=ibusGIT_PAGER=catPWD=/home/wallabot/Documentos/webCLICOLOR=1XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktopXDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdgVSCODE_CODE_CACHE_PATH=/home/wallabot/.config/Code/CachedData/6261075646f055b99068d3688932416f2346dd3bCONDA_EXE=/home/wallabot/anaconda3/bin/condaCONDA_PREFIX=/home/wallabot/anaconda3VSCODE_PID=3897
View an environment variable with the echo
command
To see a specific environment variable, we can do it using the echo
command followed by the $
symbol and the name of the variable
!echo $HOME
/home/wallabot
Modify an environment variable for a terminal session
We can modify an environment variable for the active terminal session, for example, let's add a new path to the PATH
variable. First, let's see what's in it
!echo $PATH
/home/wallabot/anaconda3/bin:/home/wallabot/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Now we add a new directory
!PATH=$PATH:"subdirectorio
We return to see what's inside PATH
!echo $PATH
/home/wallabot/anaconda3/bin:/home/wallabot/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:subdirectorio
We see that the subdirectory
directory has been added.
The problem with this method is that when we open a new terminal this change in PATH
will not be maintained.
Modify an environment variable for all terminal sessions
We go to the home folder
terminal("cd /home/wallabot")
Here in the home, we list all files with the -a
(all) flag
terminal("cd /home/wallabot")!ls -a
. .eclipse .pki.. Escritorio Plantillas.afirma .gitconfig .platformioanaconda3 .gnupg .profile.audacity-data Imágenes .psensor.bash_history .ipython Público.bash_logout .java .python_history.bashrc .jupyter snap.cache .lesshst .ssh.conda Lightworks .sudo_as_admin_successful.config .Lightworks.thereCanBeOnlyOne .thunderbird.cortex-debug .local Vídeos.cyberghost logiops .vnc.dbus .MCTranscodingSDK .vscodeDescargas .mozilla .wget-hsts.docker MúsicaDocumentos .nv
We see that there is a file called .bashrc
, this file is the file that contains the configuration of our bash
terminal("cat .bashrc", max_lines_output=3)
# ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples...fiunset __conda_setup# <<< conda initialize <<<
This file configures the terminal each time a new one is opened, so if we edit the PATH
variable in it, this change will persist for all new terminal windows we open.
To modify the PATH
variable within the configuration file, we need to add the following line to the file
PATH=$PATH:"subdirectory"```
Create aliases for all sessions
We already saw how to create command aliases, but it also happened that they were lost every time we closed a terminal session. To prevent this, we also add them to the .bashrc
configuration file. For example, in my case, I have added the following lines.
alias ll='ls -l'alias la='ls -a'
bash
alias lh='ls -h'
alias
Search Commands
Searching for Binaries with which
The first search command we will see is which
which allows us to find the path of the binaries
!which python
/home/wallabot/anaconda3/bin/python
However, if we look for something that is not in any of the PATH routes, which
will not be able to tell us the path.
!which cd
File Search with find
To search for a file with find, we need to indicate from which path we want to search for the file, followed by the -name
flag and the name of the file we want to search for.
!which cd!find ~ -name "2021-02-11-Introduccion-a-Python.ipynb"
/home/wallabot/Documentos/web/portafolio/posts/prueba/2021-02-11-Introduccion-a-Python.ipynb/home/wallabot/Documentos/web/portafolio/posts/2021-02-11-Introduccion-a-Python.ipynb
As we can see, it is in its directory plus the copy that I have created in this notebook and saved in the prueba
folder
One very powerful thing about find
is that we can use wildcards
, for example, if I want to search for all text files in my web
folder.
!find ~/Documentos/web/ -name *.txt
/home/wallabot/Documentos/web/portafolio/posts/2022-09-12 Introduccion a la terminal.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/lista.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/dot.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/dot2.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/secuential.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio/rootfile.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio/mitexto.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/file.txt/home/wallabot/Documentos/web/wordpress_api_rest/page.txt
If we do not want it to distinguish between uppercase and lowercase, we must use the -iname
flag. For example, if we search for all files that contain the text FILE
, but using the -iname
flag.
!find ~/Documentos/web/ -iname *FILE*
/home/wallabot/Documentos/web/portafolio/posts/html_files/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio/rootfile.txt/home/wallabot/Documentos/web/portafolio/posts/prueba/file.txt
We see that all the results contain file
and not FILE
, that is, it has not distinguished between uppercase and lowercase
We can specify the file type with the -type
flag. It only accepts two types f
for files and d
for directories.
!find ~/Documentos/nerf -name image*
/home/wallabot/Documentos/nerf/instant-ngp/configs/image/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/benchmarks/image/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/dependencies/cutlass/media/images/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/dependencies/fmt/doc/bootstrap/mixins/image.less/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/data/images/home/wallabot/Documentos/nerf/instant-ngp/dependencies/dlss/NVIDIAImageScaling/samples/media/images/home/wallabot/Documentos/nerf/instant-ngp/data/nerf/fox/images/home/wallabot/Documentos/nerf/instant-ngp/data/image
!find ~/Documentos/nerf -name image* -type d
/home/wallabot/Documentos/nerf/instant-ngp/configs/image/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/benchmarks/image/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/dependencies/cutlass/media/images/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/data/images/home/wallabot/Documentos/nerf/instant-ngp/dependencies/dlss/NVIDIAImageScaling/samples/media/images/home/wallabot/Documentos/nerf/instant-ngp/data/nerf/fox/images/home/wallabot/Documentos/nerf/instant-ngp/data/image
!find ~/Documentos/nerf -name image* -type f
/home/wallabot/Documentos/nerf/instant-ngp/dependencies/tiny-cuda-nn/dependencies/fmt/doc/bootstrap/mixins/image.less
If we want to filter by file size we can use the -size
flag, for example, if we want to search for all files larger than 200 MB
!find ~/Documentos/ -type f -size +200M
/home/wallabot/Documentos/kaggle/hubmap/models/13_efficientnet-b7_final_model.pth/home/wallabot/Documentos/kaggle/hubmap/models/12_efficientnet-b7_final_model.pth/home/wallabot/Documentos/kaggle/hubmap/models/14_resnet152_final_model.pth/home/wallabot/Documentos/kaggle/hubmap/models/14_resnet152_best_model.pth/home/wallabot/Documentos/kaggle/hubmap/models/12_efficientnet-b7_early_stopping.pth/home/wallabot/Documentos/kaggle/hubmap/models/efficientnet-b7-dcc49843.pth/home/wallabot/Documentos/kaggle/hubmap/models/13_efficientnet-b7_early_stopping.pth/home/wallabot/Documentos/kaggle/hubmap/models/14_resnet152_early_stopping.pth/home/wallabot/Documentos/kaggle/hubmap/models/12_efficientnet-b7_best_model.pth/home/wallabot/Documentos/kaggle/hubmap/models/13_efficientnet-b7_best_model.pth
If we want to perform operations after the search, we use the -exec
flag
For example, I am going to search for all folders named subdirectorio
!find ~/ -name subdirectorio -type d
/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio
I can make them delete with the -exec
flag
!find ~/ -name subdirectorio -type d -exec rm -r {} ;
rm: ¿borrar el fichero regular vacío '/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio/rootfile.txt' protegido contra escritura? (s/n) sfind: ‘/home/wallabot/Documentos/web/portafolio/posts/prueba/subdirectorio’: No existe el archivo o el directorio
!find ~/ -name subdirectorio -type d
Finally, if we use the !
character, we will be indicating that it should find everything that does not match what we have specified.
!find ~/ -name subdirectorio -type d!find ~/Documentos/web/portafolio/posts/prueba ! -name *.txt
/home/wallabot/Documentos/web/portafolio/posts/prueba/home/wallabot/Documentos/web/portafolio/posts/prueba/index.html/home/wallabot/Documentos/web/portafolio/posts/prueba/Abc/home/wallabot/Documentos/web/portafolio/posts/prueba/datos1/home/wallabot/Documentos/web/portafolio/posts/prueba/2021-02-11-Introduccion-a-Python.ipynb/home/wallabot/Documentos/web/portafolio/posts/prueba/datos123
As we can see, it has found everything that is not a .txt
grep
Search Command
grep
is a very powerful search command, that's why we dedicate a section to it alone. The grep
command uses regular expressions, so if you want to learn about them, I leave you a link to a post where I explain them.
Let's start to see the power of this command, let's search for all the times the text MaximoFN
appears within the file 2021-02-11-Introduccion-a-Python.ipynb
terminal("cd /home/wallabot/Documentos/web/portafolio/posts/prueba")
terminal("cd /home/wallabot/Documentos/web/portafolio/posts/prueba")terminal("grep MaximoFN 2021-02-11-Introduccion-a-Python.ipynb", max_lines_output=20)
"a = 'MaximoFN' ","'MaximoFN'""string = "MaximoFN" ","'MaximoFN'""string = 'MaximoFN' ","'MaximoFN'""Este es el blog de "MaximoFN" ""print("Este es el blog de \"MaximoFN\"")""Este es el blog de 'MaximoFN' ""print('Este es el blog de \'MaximoFN\'')""Este es el blog de \MaximoFN\\n""print('Este es el blog de \\MaximoFN\\\')""MaximoFN ""print('Este es el blog de \nMaximoFN')""Este es el blog de MaximoFN ""print('Esto no se imprimirá \rEste es el blog de MaximoFN')""Este es el blog de MaximoFN ""print('Este es el blog de \tMaximoFN')""Este es el blog deMaximoFN ""print('Este es el blog de \bMaximoFN')"..."funcion2_del_modulo('MaximoFN')""MaximoFN "," print('MaximoFN') "," variable = 'MaximoFN' ",
However, if we perform the same search for the text maximofn
!grep maximofn 2021-02-11-Introduccion-a-Python.ipynb
No results appear, this is because grep
is case sensitive
, that is, it searches the text exactly as you have entered it, differentiating between uppercase and lowercase. If we don't want this, we need to introduce the -i
flag.
!grep maximofn 2021-02-11-Introduccion-a-Python.ipynbterminal("grep -i MaximoFN 2021-02-11-Introduccion-a-Python.ipynb", max_lines_output=20)
"a = 'MaximoFN' ","'MaximoFN'""string = "MaximoFN" ","'MaximoFN'""string = 'MaximoFN' ","'MaximoFN'""Este es el blog de "MaximoFN" ""print("Este es el blog de \"MaximoFN\"")""Este es el blog de 'MaximoFN' ""print('Este es el blog de \'MaximoFN\'')""Este es el blog de \MaximoFN\\n""print('Este es el blog de \\MaximoFN\\\')""MaximoFN ""print('Este es el blog de \nMaximoFN')""Este es el blog de MaximoFN ""print('Esto no se imprimirá \rEste es el blog de MaximoFN')""Este es el blog de MaximoFN ""print('Este es el blog de \tMaximoFN')""Este es el blog deMaximoFN ""print('Este es el blog de \bMaximoFN')"..."funcion2_del_modulo('MaximoFN')""MaximoFN "," print('MaximoFN') "," variable = 'MaximoFN' ",
If what we want is for it to return the number of times it appears, we use the -c
flag
!grep -c MaximoFN 2021-02-11-Introduccion-a-Python.ipynb
105
If we don't care if it appears in uppercase or lowercase, we can add the -i
flag again, but there's no need to separate it from the -c
flag; they can be introduced together
!grep -ci MaximoFN 2021-02-11-Introduccion-a-Python.ipynb
105
If now we want all the times in which the word MáximoFN
does not appear, we introduce the -v
flag
!grep -cv MaximoFN 2021-02-11-Introduccion-a-Python.ipynb
11573
Network Utilities
Network Interface Information with ifconfig
The first command will be ifconfig
which shows us information about our network interfaces.
!ifconfig
br-470e52ae2708: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255ether 02:42:ac:d0:b9:eb txqueuelen 0 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255ether 02:42:5d:15:1c:e9 txqueuelen 0 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0enp6s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.1.144 netmask 255.255.255.0 broadcast 192.168.1.255inet6 fe80::7dc2:6944:3fbe:c18e prefixlen 64 scopeid 0x20<link>ether 24:4b:fe:5c:f6:59 txqueuelen 1000 (Ethernet)RX packets 144369 bytes 123807349 (123.8 MB)RX errors 0 dropped 2056 overruns 0 frame 0TX packets 100672 bytes 55678042 (55.6 MB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Bucle local)RX packets 10748 bytes 1832545 (1.8 MB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 10748 bytes 1832545 (1.8 MB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0wlp5s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500ether 4c:77:cb:1d:66:cc txqueuelen 1000 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
As we can see, we have information about all the network interfaces of my computer, but if I want to know only one, it is specified by adding its name.
!ifconfig enp6s0
enp6s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.1.144 netmask 255.255.255.0 broadcast 192.168.1.255inet6 fe80::7dc2:6944:3fbe:c18e prefixlen 64 scopeid 0x20<link>ether 24:4b:fe:5c:f6:59 txqueuelen 1000 (Ethernet)RX packets 144467 bytes 123842258 (123.8 MB)RX errors 0 dropped 2060 overruns 0 frame 0TX packets 100786 bytes 55749109 (55.7 MB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Network Interface Information with ip
Another way to obtain information about our network interfaces is by using the ip
command; adding a
gives us information about all interfaces.
!ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: enp6s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 24:4b:fe:5c:f6:59 brd ff:ff:ff:ff:ff:ffinet 192.168.1.144/24 brd 192.168.1.255 scope global dynamic noprefixroute enp6s0valid_lft 80218sec preferred_lft 80218secinet6 fe80::7dc2:6944:3fbe:c18e/64 scope link noprefixroutevalid_lft forever preferred_lft forever3: wlp5s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000link/ether 4c:77:cb:1d:66:cc brd ff:ff:ff:ff:ff:ff4: br-470e52ae2708: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:ac:d0:b9:eb brd ff:ff:ff:ff:ff:ffinet 172.18.0.1/16 brd 172.18.255.255 scope global br-470e52ae2708valid_lft forever preferred_lft forever5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:5d:15:1c:e9 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft forever
Communication Test with ping
Another useful command is ping
, which can help us see if we have a connection to a specific IP
. For example, Google's IP
is 142.250.200.78
, so we do a ping
to see if it responds. The ping
command in Linux pings continuously, so it never ends until we stop it. To prevent this, we add the -c
flag and the number of attempts.
!ping 142.250.200.132 -c 4
PING 142.250.200.132 (142.250.200.132) 56(84) bytes of data.64 bytes from 142.250.200.132: icmp_seq=1 ttl=117 time=3.46 ms64 bytes from 142.250.200.132: icmp_seq=2 ttl=117 time=3.77 ms64 bytes from 142.250.200.132: icmp_seq=3 ttl=117 time=2.81 ms64 bytes from 142.250.200.132: icmp_seq=4 ttl=117 time=2.86 ms--- 142.250.200.132 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3004msrtt min/avg/max/mdev = 2.812/3.227/3.773/0.405 ms
The same would have happened if we had done it directly on google.com
!ping www.google.com -c 4
PING www.google.com (142.250.200.132) 56(84) bytes of data.64 bytes from mad41s14-in-f4.1e100.net (142.250.200.132): icmp_seq=1 ttl=117 time=2.74 ms64 bytes from mad41s14-in-f4.1e100.net (142.250.200.132): icmp_seq=2 ttl=117 time=3.96 ms64 bytes from mad41s14-in-f4.1e100.net (142.250.200.132): icmp_seq=3 ttl=117 time=3.56 ms64 bytes from mad41s14-in-f4.1e100.net (142.250.200.132): icmp_seq=4 ttl=117 time=2.87 ms--- www.google.com ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3003msrtt min/avg/max/mdev = 2.741/3.283/3.962/0.499 ms
Download Source Files with curl
We can obtain a text file from a given address using the curl
command, for example, we can download Google's html.
!curl https://www.google.com
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="es"><head><meta content="Google.es permite acceder a la informaci�n mundial en castellano, catal�n, gallego, euskara e ingl�s." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){window.google={kEI:'M5GOY6PeLr-jkdUP1pir0AE',kEXPI:'0,1359409,6059,206,4804,2316,383,246,5,5367,1123753,1197713,688,380089,16115,28684,22430,1362,12312,17587,4998,13228,3847,10622,22741,5081,1593,1279,2742,149,1103,840,1983,214,4100,3514,606,2023,2297,14670,3227,2845,7,4773,28997,1850,15757,3,346,230,6459,149,13975,4,1528,2304,7039,27731,7357,13658,4437,16786,5815,2542,4094,4052,3,3541,1,14262,27892,2,14022,6248,19490,5680,1021,2380,28741,4569,6255,23421,1252,5835,14967,4333,7484,11406,15676,8155,7381,15970,873,14804,1,4828,7,1922,5784,12208,10330,587,12192,4832,26504,5796,3,14433,3890,751,13384,1499,3,679,1622,1779,1886,338,1627,1119,6,8909,80,243,458,3438,1763,722,1020,813,91,1133,10,280,2306,44,77,1420,3,562,402,314,275,2095,440,399,138,384,1033,334,2667,2,723,444,79,403,501,929,3,785,2,240,78,2022,284,196,732,175,342,244,617,335,1,841,1275,14,979,57,857,446,2,1900,838,251,227,50,21,8,3,442,57,40,936,697,773,95,121,643,1502,163,355,702,195,1,452,50,334,687,109,1,19,109,134,546,80,5,36,124,68,135,131,415,47,27,266,563,48,231,742,15,527,2,6,495,1,495,5,62,1627,441,262,5,3,648,3,6,4,13,39,538,792,337,9,115,98,180,148,308,401,1240,2,726,243,2044,5286450,84,19,32,115,11,70,5995534,2803414,3311,141,795,19735,1,1,346,1755,1004,41,342,1,189,14,1,10,8,1,5,4,2,1,3,2,2,1,3,1,3,1,4,3,1,3,2,2,23947076,511,21,11,4041599,1964,1007,2087,13579,3102,303,5595,11,3835,3637,2623,9,136,1524825',kBL:'p9Xv'};google.sn='webhp';google.kHL='es';})();(function(){var f=this||self;var h,k=[];function l(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||h}function m(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}function n(a,b,c,d,g){var e="";c||-1!==b.search("&ei=")||(e="&ei="+l(d),-1===b.search("&lei=")&&(d=m(d))&&(e+="&lei="+d));d="";!c&&f._cshid&&-1===b.search("&cshid=")&&"slh"!==a&&(d="&cshid="+f._cshid);c=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+"&zx="+Date.now()+d;/^http:/i.test(c)&&"https:"===window.location.protocol&&(google.ml&&google.ml(Error("a"),!1,{src:c,glmm:1}),c="");return c};h=google.kEI;google.getEI=l;google.getLEI=m;google.ml=function(){return null};google.log=function(a,b,c,d,g){if(c=n(a,b,c,d,g)){a=new Image;var e=k.length;k[e]=a;a.onerror=a.onload=a.onabort=function(){delete k[e]};a.src=c}};google.logUrl=n;}).call(this);(function(){google.y={};google.sy=[];google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.sx=function(a){google.sy.push(a)};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};google.bx=!1;google.lx=function(){};}).call(this);google.f={};(function(){document.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"===c||"q"===c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);document.documentElement.addEventListener("click",function(b){var a;a:{for(a=b.target;a&&a!==document.documentElement;a=a.parentElement)if("A"===a.tagName){a="1"===a.getAttribute("data-nohref");break a}a=!1}a&&b.preventDefault()},!0);}).call(this);</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#1558d6}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:#000}a{color:#4b11a8;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#1558d6}a:visited{color:#4b11a8}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#f8f9fa;border:solid 1px;border-color:#dadce0 #70757a #70757a #dadce0;height:30px}.lsbb{display:block}#WqQANb a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#dadce0}.lst:focus{outline:none}</style><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){window.google.erd={jsr:1,bv:1698,de:true};var h=this||self;var k,l=null!=(k=h.mei)?k:1,n,p=null!=(n=h.sdo)?n:!0,q=0,r,t=google.erd,v=t.jsr;google.ml=function(a,b,d,m,e){e=void 0===e?2:e;b&&(r=a&&a.message);if(google.dl)return google.dl(a,e,d),null;if(0>v){window.console&&console.error(a,d);if(-2===v)throw a;b=!1}else b=!a||!a.message||"Error loading script"===a.message||q>=l&&!m?!1:!0;if(!b)return null;q++;d=d||{};b=encodeURIComponent;var c="/gen_204?atyp=i&ei="+b(google.kEI);google.kEXPI&&(c+="&jexpid="+b(google.kEXPI));c+="&srcpg="+b(google.sn)+"&jsr="+b(t.jsr)+"&bver="+b(t.bv);var f=a.lineNumber;void 0!==f&&(c+="&line="+f);var g=a.fileName;g&&(0<g.indexOf("-extension:/")&&(e=3),c+="&script="+b(g),f&&g===window.location.href&&(f=document.documentElement.outerHTML.split(" ")[f],c+="&cad="+b(f?f.substring(0,300):"No script found.")));c+="&jsel="+e;for(var u in d)c+="&",c+=b(u),c+="=",c+=b(d[u]);c=c+"&emsg="+b(a.name+": "+a.message);c=c+"&jsst="+b(a.stack||"N/A");12288<=c.length&&(c=c.substr(0,12288));a=c;m||google.log(0,"",a);return a};window.onerror=function(a,b,d,m,e){r!==a&&(a=e instanceof Error?e:Error(a),void 0===d||"lineNumber"in a||(a.lineNumber=d),void 0===b||"fileName"in a||(a.fileName=b),google.ml(a,!1,void 0,!1,"SyntaxError"===a.name||"SyntaxError"===a.message.substring(0,11)||-1!==a.message.indexOf("Script error")?3:0));r=null;p&&q>=l&&(window.onerror=null)};})();</script></head><body bgcolor="#fff"><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}})();</script><div id="mngb"><div id=gbar><nobr><b class=gb1>B�squeda</b> <a class=gb1 href="https://www.google.es/imghp?hl=es&tab=wi">Im�genes</a> <a class=gb1 href="https://maps.google.es/maps?hl=es&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=es&tab=w8">Play</a> <a class=gb1 href="https://www.youtube.com/?tab=w1">YouTube</a> <a class=gb1 href="https://news.google.com/?tab=wn">Noticias</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="https://www.google.es/intl/es/about/products?tab=wh"><u>M�s</u> »</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.es/history/optout?hl=es" class=gb4>Historial web</a> | <a href="/preferences?hl=es" class=gb4>Ajustes</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=es&passive=true&continue=https://www.google.com/&ec=GAZAAQ" class=gb4>Iniciar sesi�n</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%"> </td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="es" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><input class="lst" style="margin:0;padding:5px 8px 0 6px;vertical-align:top;color:#000" autocomplete="off" value="" title="Buscar con Google" maxlength="2048" name="q" size="57"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Buscar con Google" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" id="tsuid_1" value="Voy a tener suerte" name="btnI" type="submit"><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){var id='tsuid_1';document.getElementById(id).onclick = function(){if (this.form.q.value){this.checked = 1;if (this.form.iflsig)this.form.iflsig.disabled = false;}else top.location='/doodles/';};})();</script><input value="AJiK0e8AAAAAY46fQwdyVrbrgW6gkEtVkGfp2nyO0ZXL" name="iflsig" type="hidden"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=es&authuser=0">B�squeda avanzada</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){var a,b="1";if(document&&document.getElementById)if("undefined"!=typeof XMLHttpRequest)b="2";else if("undefined"!=typeof ActiveXObject){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if("2"==a&&-1==location.search.indexOf("&gbv=2")){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="gws-output-pages-elements-homepage_additional_languages__als"><style>#gws-output-pages-elements-homepage_additional_languages__als{font-size:small;margin-bottom:24px}#SIvCob{color:#3c4043;display:inline-block;line-height:28px;}#SIvCob a{padding:0 3px;}.H6sW5{display:inline-block;margin:0 2px;white-space:nowrap}.z4hgWe{display:inline-block;margin:0 2px}</style><div id="SIvCob">Ofrecido por Google en: <a href="https://www.google.com/setprefs?sig=0_vwUKUD2Xhro4NnrueK1hCfItt30%3D&hl=ca&source=homepage&sa=X&ved=0ahUKEwjjw_C44uP7AhW_UaQEHVbMChoQ2ZgBCAU">catal�</a> <a href="https://www.google.com/setprefs?sig=0_vwUKUD2Xhro4NnrueK1hCfItt30%3D&hl=gl&source=homepage&sa=X&ved=0ahUKEwjjw_C44uP7AhW_UaQEHVbMChoQ2ZgBCAY">galego</a> <a href="https://www.google.com/setprefs?sig=0_vwUKUD2Xhro4NnrueK1hCfItt30%3D&hl=eu&source=homepage&sa=X&ved=0ahUKEwjjw_C44uP7AhW_UaQEHVbMChoQ2ZgBCAc">euskara</a> </div></div></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="WqQANb"><a href="http://www.google.es/intl/es/services/">Soluciones Empresariales</a><a href="/intl/es/about.html">Todo acerca de Google</a><a href="https://www.google.com/setprefdomain?prefdom=ES&prev=https://www.google.es/&sig=K_a2UXepORMQOw5-SHU8h4noB_VWk%3D">Google.es</a></div></div><p style="font-size:8pt;color:#70757a">© 2022 - <a href="/intl/es/policies/privacy/">Privacidad</a> - <a href="/intl/es/policies/terms/">T�rminos</a></p></span></center><script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){window.google.cdo={height:757,width:1440};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();</script> <script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){google.xjs={ck:'xjs.hp.oxai9SxkIQY.L.X.O',cs:'ACT90oEGh-_ImDfBjn6aD_ABGaOlD2MqVw',excm:[]};})();</script> <script nonce="zXcc4tMJWBRoE7q_o_Z2fQ">(function(){var u='/xjs/_/js/k=xjs.hp.en.9b-uVUIpJU8.O/am=AADoBABQAGAB/d=1/ed=1/rs=ACT90oG-6KYVksw4jxVvNcwan406xE6qVw/m=sb_he,d';var amd=0;var d=this||self,e=function(a){return a};var g;var l=function(a,b){this.g=b===h?a:""};l.prototype.toString=function(){return this.g+""};var h={};function m(){var a=u;google.lx=function(){p(a);google.lx=function(){}};google.bx||google.lx()}function p(a){google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");var b=document;var c="SCRIPT";"application/xhtml+xml"===b.contentType&&(c=c.toLowerCase());c=b.createElement(c);if(void 0===g){b=null;var k=d.trustedTypes;if(k&&k.createPolicy){try{b=k.createPolicy("goog#html",{createHTML:e,createScript:e,createScriptURL:e})}catch(q){d.console&&d.console.error(q.message)}g=b}else g=b}a=(b=g)?b.createScriptURL(a):a;a=new l(a,h);c.src=a instanceof l&&a.constructor===l?a.g:"type_error:TrustedResourceUrl";var f,n;(f=(a=null==(n=(f=(c.ownerDocument&&c.ownerDocument.defaultView||window).document).querySelector)?void 0:n.call(f,"script[nonce]"))?a.nonce||a.getAttribute("nonce")||"":"")&&c.setAttribute("nonce",f);document.body.appendChild(c);google.psa=!0};google.xjsu=u;setTimeout(function(){0<amd?google.caft(function(){return m()},amd):m()},0);})();function _DumpException(e){throw e;}function _F_installCss(c){}(function(){google.jl={blt:'none',chnk:0,dw:false,dwu:true,emtn:0,end:0,ico:false,ikb:0,ine:false,injs:'none',injt:0,injth:0,injv2:false,lls:'default',pdt:0,rep:0,snet:true,strt:0,ubm:false,uwp:true};})();(function(){var pmc='{"d":{},"sb_he":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","fl":true,"host":"google.com","jsonp":true,"lm":true,"msgs":{"cibl":"Borrar b�squeda","dym":"Quiz�s quisiste decir:","lcky":"Voy a tener suerte","lml":"M�s informaci�n","psrc":"Esta b�squeda se ha eliminado de tu \u003Ca href=\"/history\"\u003Ehistorial web\u003C/a\u003E.","psrl":"Eliminar","sbit":"Buscar por imagen","srch":"Buscar con Google"},"ovr":{},"pq":"","rfs":[],"sbas":"0 3px 8px 0 rgba(0,0,0,0.2),0 0 0 1px rgba(0,0,0,0.08)","stok":"gh8wSanWNWQy8f-PH0wGTjDkvYQ"}}';google.pmc=JSON.parse(pmc);})();</script> </body></html>
We can also create a pipeline to save it to a file
!curl https://www.google.com > google.html
% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 15168 0 15168 0 0 135k 0 --:--:-- --:--:-- --:--:-- 137k
Now we can see if it has saved correctly
!cat google.html
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="es"><head><meta content="Google.es permite acceder a la informaci�n mundial en castellano, catal�n, gallego, euskara e ingl�s." name="description"><meta content="noodp" name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){window.google={kEI:'R5GOY-LZHLegkdUP_IqzoAE',kEXPI:'0,1359409,6059,206,4804,2316,383,246,5,5367,1123753,1197777,380713,16115,28684,22430,1362,283,12036,17580,4998,13228,516,3331,10622,22741,5081,1593,1279,2742,149,1103,840,1983,4,210,4100,3514,606,2023,2299,14668,3229,2843,7,4773,826,23475,4696,1851,15756,3,346,230,6459,149,13975,4,1528,2304,7039,20309,7422,7357,13658,4437,16786,5812,2545,4094,4052,3,3541,1,11943,30211,2,8984,1,5037,6249,19490,5679,1020,2378,28745,4568,6258,23418,1252,5835,14967,4333,4239,3245,27082,239,7916,7381,15969,874,19633,6,1923,5784,3995,21779,1120,8423,4832,26080,423,107,5690,3,14433,3890,751,14879,3,683,217,1405,1779,1854,31,1966,1119,6,8909,323,5659,1741,814,1224,10,280,2346,82,1419,3,565,401,519,68,970,1125,440,398,156,367,1034,333,3392,526,396,3,1431,3,785,2,312,2312,196,907,342,244,618,314,1,293,568,171,1104,14,89,891,56,857,306,14,509,154,246,1110,219,628,249,229,49,8,8,3,55,4,399,55,39,1072,49,43,2,468,782,83,123,641,1502,166,350,707,195,5,140,358,329,692,109,1,20,108,134,547,67,5,49,93,31,77,124,79,355,160,27,829,236,764,12,35,118,98,803,1,65,436,5,5,54,2065,262,5,3,647,3,8,2,14,39,65,380,80,14,790,346,115,99,1323,4,711,242,2,723,2286,5280608,12,5934,147,81,8798948,3311,141,795,19735,1,1,346,1755,1004,41,342,1,189,14,9,4,6,3,3,4,1,2,2,3,2,2,2,1,2,5,2,2,1,2,2,2,23947077,512,18,13,2737921,1303678,1964,3094,13579,3405,5595,11,3835,1923,3208,1069,1480676,40778',kBL:'p9Xv'};google.sn='webhp';google.kHL='es';})();(function(){var f=this||self;var h,k=[];function l(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||h}function m(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}function n(a,b,c,d,g){var e="";c||-1!==b.search("&ei=")||(e="&ei="+l(d),-1===b.search("&lei=")&&(d=m(d))&&(e+="&lei="+d));d="";!c&&f._cshid&&-1===b.search("&cshid=")&&"slh"!==a&&(d="&cshid="+f._cshid);c=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+e+"&zx="+Date.now()+d;/^http:/i.test(c)&&"https:"===window.location.protocol&&(google.ml&&google.ml(Error("a"),!1,{src:c,glmm:1}),c="");return c};h=google.kEI;google.getEI=l;google.getLEI=m;google.ml=function(){return null};google.log=function(a,b,c,d,g){if(c=n(a,b,c,d,g)){a=new Image;var e=k.length;k[e]=a;a.onerror=a.onload=a.onabort=function(){delete k[e]};a.src=c}};google.logUrl=n;}).call(this);(function(){google.y={};google.sy=[];google.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1};google.sx=function(a){google.sy.push(a)};google.lm=[];google.plm=function(a){google.lm.push.apply(google.lm,a)};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};google.bx=!1;google.lx=function(){};}).call(this);google.f={};(function(){document.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"===c||"q"===c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);document.documentElement.addEventListener("click",function(b){var a;a:{for(a=b.target;a&&a!==document.documentElement;a=a.parentElement)if("A"===a.tagName){a="1"===a.getAttribute("data-nohref");break a}a=!1}a&&b.preventDefault()},!0);}).call(this);</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#1558d6}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:#000}a{color:#4b11a8;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#1558d6}a:visited{color:#4b11a8}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#f8f9fa;border:solid 1px;border-color:#dadce0 #70757a #70757a #dadce0;height:30px}.lsbb{display:block}#WqQANb a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#dadce0}.lst:focus{outline:none}</style><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){window.google.erd={jsr:1,bv:1698,de:true};var h=this||self;var k,l=null!=(k=h.mei)?k:1,n,p=null!=(n=h.sdo)?n:!0,q=0,r,t=google.erd,v=t.jsr;google.ml=function(a,b,d,m,e){e=void 0===e?2:e;b&&(r=a&&a.message);if(google.dl)return google.dl(a,e,d),null;if(0>v){window.console&&console.error(a,d);if(-2===v)throw a;b=!1}else b=!a||!a.message||"Error loading script"===a.message||q>=l&&!m?!1:!0;if(!b)return null;q++;d=d||{};b=encodeURIComponent;var c="/gen_204?atyp=i&ei="+b(google.kEI);google.kEXPI&&(c+="&jexpid="+b(google.kEXPI));c+="&srcpg="+b(google.sn)+"&jsr="+b(t.jsr)+"&bver="+b(t.bv);var f=a.lineNumber;void 0!==f&&(c+="&line="+f);var g=a.fileName;g&&(0<g.indexOf("-extension:/")&&(e=3),c+="&script="+b(g),f&&g===window.location.href&&(f=document.documentElement.outerHTML.split(" ")[f],c+="&cad="+b(f?f.substring(0,300):"No script found.")));c+="&jsel="+e;for(var u in d)c+="&",c+=b(u),c+="=",c+=b(d[u]);c=c+"&emsg="+b(a.name+": "+a.message);c=c+"&jsst="+b(a.stack||"N/A");12288<=c.length&&(c=c.substr(0,12288));a=c;m||google.log(0,"",a);return a};window.onerror=function(a,b,d,m,e){r!==a&&(a=e instanceof Error?e:Error(a),void 0===d||"lineNumber"in a||(a.lineNumber=d),void 0===b||"fileName"in a||(a.fileName=b),google.ml(a,!1,void 0,!1,"SyntaxError"===a.name||"SyntaxError"===a.message.substring(0,11)||-1!==a.message.indexOf("Script error")?3:0));r=null;p&&q>=l&&(window.onerror=null)};})();</script></head><body bgcolor="#fff"><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}}})();</script><div id="mngb"><div id=gbar><nobr><b class=gb1>B�squeda</b> <a class=gb1 href="https://www.google.es/imghp?hl=es&tab=wi">Im�genes</a> <a class=gb1 href="https://maps.google.es/maps?hl=es&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=es&tab=w8">Play</a> <a class=gb1 href="https://www.youtube.com/?tab=w1">YouTube</a> <a class=gb1 href="https://news.google.com/?tab=wn">Noticias</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="https://www.google.es/intl/es/about/products?tab=wh"><u>M�s</u> »</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.es/history/optout?hl=es" class=gb4>Historial web</a> | <a href="/preferences?hl=es" class=gb4>Ajustes</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=es&passive=true&continue=https://www.google.com/&ec=GAZAAQ" class=gb4>Iniciar sesi�n</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="lga"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%"> </td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="es" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><input class="lst" style="margin:0;padding:5px 8px 0 6px;vertical-align:top;color:#000" autocomplete="off" value="" title="Buscar con Google" maxlength="2048" name="q" size="57"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Buscar con Google" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" id="tsuid_1" value="Voy a tener suerte" name="btnI" type="submit"><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){var id='tsuid_1';document.getElementById(id).onclick = function(){if (this.form.q.value){this.checked = 1;if (this.form.iflsig)this.form.iflsig.disabled = false;}else top.location='/doodles/';};})();</script><input value="AJiK0e8AAAAAY46fV7gpXBHCT6KAebFZAqGv1l-4BtIR" name="iflsig" type="hidden"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=es&authuser=0">B�squeda avanzada</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){var a,b="1";if(document&&document.getElementById)if("undefined"!=typeof XMLHttpRequest)b="2";else if("undefined"!=typeof ActiveXObject){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if("2"==a&&-1==location.search.indexOf("&gbv=2")){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br><div id="gws-output-pages-elements-homepage_additional_languages__als"><style>#gws-output-pages-elements-homepage_additional_languages__als{font-size:small;margin-bottom:24px}#SIvCob{color:#3c4043;display:inline-block;line-height:28px;}#SIvCob a{padding:0 3px;}.H6sW5{display:inline-block;margin:0 2px;white-space:nowrap}.z4hgWe{display:inline-block;margin:0 2px}</style><div id="SIvCob">Ofrecido por Google en: <a href="https://www.google.com/setprefs?sig=0_HljXEzVisqsnlJP1S5dx0Fao0Lw%3D&hl=ca&source=homepage&sa=X&ved=0ahUKEwiimaPC4uP7AhU3UKQEHXzFDBQQ2ZgBCAU">catal�</a> <a href="https://www.google.com/setprefs?sig=0_HljXEzVisqsnlJP1S5dx0Fao0Lw%3D&hl=gl&source=homepage&sa=X&ved=0ahUKEwiimaPC4uP7AhU3UKQEHXzFDBQQ2ZgBCAY">galego</a> <a href="https://www.google.com/setprefs?sig=0_HljXEzVisqsnlJP1S5dx0Fao0Lw%3D&hl=eu&source=homepage&sa=X&ved=0ahUKEwiimaPC4uP7AhU3UKQEHXzFDBQQ2ZgBCAc">euskara</a> </div></div></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="WqQANb"><a href="http://www.google.es/intl/es/services/">Soluciones Empresariales</a><a href="/intl/es/about.html">Todo acerca de Google</a><a href="https://www.google.com/setprefdomain?prefdom=ES&prev=https://www.google.es/&sig=K_8O8QHBmoai9DOT5YZxMWevJK8VI%3D">Google.es</a></div></div><p style="font-size:8pt;color:#70757a">© 2022 - <a href="/intl/es/policies/privacy/">Privacidad</a> - <a href="/intl/es/policies/terms/">T�rminos</a></p></span></center><script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){window.google.cdo={height:757,width:1440};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);}).call(this);})();</script> <script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){google.xjs={ck:'xjs.hp.oxai9SxkIQY.L.X.O',cs:'ACT90oEGh-_ImDfBjn6aD_ABGaOlD2MqVw',excm:[]};})();</script> <script nonce="Jo7WFU6XWWwu6NrdwaRyIw">(function(){var u='/xjs/_/js/k=xjs.hp.en.9b-uVUIpJU8.O/am=AADoBABQAGAB/d=1/ed=1/rs=ACT90oG-6KYVksw4jxVvNcwan406xE6qVw/m=sb_he,d';var amd=0;var d=this||self,e=function(a){return a};var g;var l=function(a,b){this.g=b===h?a:""};l.prototype.toString=function(){return this.g+""};var h={};function m(){var a=u;google.lx=function(){p(a);google.lx=function(){}};google.bx||google.lx()}function p(a){google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");var b=document;var c="SCRIPT";"application/xhtml+xml"===b.contentType&&(c=c.toLowerCase());c=b.createElement(c);if(void 0===g){b=null;var k=d.trustedTypes;if(k&&k.createPolicy){try{b=k.createPolicy("goog#html",{createHTML:e,createScript:e,createScriptURL:e})}catch(q){d.console&&d.console.error(q.message)}g=b}else g=b}a=(b=g)?b.createScriptURL(a):a;a=new l(a,h);c.src=a instanceof l&&a.constructor===l?a.g:"type_error:TrustedResourceUrl";var f,n;(f=(a=null==(n=(f=(c.ownerDocument&&c.ownerDocument.defaultView||window).document).querySelector)?void 0:n.call(f,"script[nonce]"))?a.nonce||a.getAttribute("nonce")||"":"")&&c.setAttribute("nonce",f);document.body.appendChild(c);google.psa=!0};google.xjsu=u;setTimeout(function(){0<amd?google.caft(function(){return m()},amd):m()},0);})();function _DumpException(e){throw e;}function _F_installCss(c){}(function(){google.jl={blt:'none',chnk:0,dw:false,dwu:true,emtn:0,end:0,ico:false,ikb:0,ine:false,injs:'none',injt:0,injth:0,injv2:false,lls:'default',pdt:0,rep:0,snet:true,strt:0,ubm:false,uwp:true};})();(function(){var pmc='{"d":{},"sb_he":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","fl":true,"host":"google.com","jsonp":true,"lm":true,"msgs":{"cibl":"Borrar b�squeda","dym":"Quiz�s quisiste decir:","lcky":"Voy a tener suerte","lml":"M�s informaci�n","psrc":"Esta b�squeda se ha eliminado de tu \u003Ca href=\"/history\"\u003Ehistorial web\u003C/a\u003E.","psrl":"Eliminar","sbit":"Buscar por imagen","srch":"Buscar con Google"},"ovr":{},"pq":"","rfs":[],"sbas":"0 3px 8px 0 rgba(0,0,0,0.2),0 0 0 1px rgba(0,0,0,0.08)","stok":"GYSMF2y7hymT0L3W0W4RPVIsSrU"}}';google.pmc=JSON.parse(pmc);})();</script> </body></html>
Download files with wget
Another similar command is wget
, however, unlike curl
, wget
downloads the file directly.
!wget https://www.google.com
--2022-12-06 01:49:19-- https://www.google.com/Resolviendo www.google.com (www.google.com)... 142.250.200.68, 2a00:1450:4003:80c::2004Conectando con www.google.com (www.google.com)[142.250.200.68]:443... conectado.Petición HTTP enviada, esperando respuesta... 200 OKLongitud: no especificado [text/html]Guardando como: “index.html.1”index.html.1 [ <=> ] 14,76K --.-KB/s en 0,002s2022-12-06 01:49:19 (7,17 MB/s) - “index.html.1” guardado [15117]
!ls -l
total 316-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 15168 dic 6 01:48 google.html-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 15117 dic 6 01:49 index.html.1-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
We see that it has been saved as index.html
, which is how Google has it named.
If we want to save it with a specific name, we can use the -O
flag
!wget https://www.google.com -O google2.html
--2022-12-06 01:49:37-- https://www.google.com/Resolviendo www.google.com (www.google.com)... 142.250.200.68, 2a00:1450:4003:80c::2004Conectando con www.google.com (www.google.com)[142.250.200.68]:443... conectado.Petición HTTP enviada, esperando respuesta... 200 OKLongitud: no especificado [text/html]Guardando como: “google2.html”google2.html [ <=> ] 14,78K --.-KB/s en 0,003s2022-12-06 01:49:37 (5,27 MB/s) - “google2.html” guardado [15131]
!ls -l
total 332-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 15131 dic 6 01:49 google2.html-rw-rw-r-- 1 wallabot wallabot 15168 dic 6 01:48 google.html-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 15117 dic 6 01:49 index.html.1-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
Path Debugging with traceroute
A very useful command is to see the route to a destination, for this we use traceroute
, for example, let's see all the sites I have to pass through to connect to Google
!traceroute www.google.com
traceroute to www.google.com (142.250.200.68), 64 hops max1 192.168.1.1 0,435ms 0,154ms 0,133ms2 188.127.176.1 3,979ms 2,914ms 3,397ms3 10.15.0.77 3,600ms 3,914ms 2,669ms4 10.15.246.6 3,567ms 3,713ms 2,926ms5 * * *6 72.14.209.84 3,981ms 2,914ms 2,993ms7 * * *8 142.251.54.148 3,856ms 2,916ms 2,905ms9 142.250.200.68 2,908ms 2,949ms 3,037ms
Route Debugging with mtr
Another debugging tool is mtr
, which is an improved version of traceroute
. It provides information for each hop, such as response time, packet loss percentage, etc.
!mtr -n maximofn.com
wallabot (192.168.178.144)Keys: Help Display mode Restart statistics Order of fields quitPackets PingsHost Loss% Snt Last Avg Best Wrst StDev1. 192.168.178.1 0.0% 345 0.3 0.3 0.3 0.3 0.02. 192.168.0.1 0.0% 344 0.8 1.1 1.1 1.1 0.03. (waiting for reply)4. 10.183.52.41 0.0% 344 2.8 2.5 2.5 2.5 0.05. 172.29.0.161 47.7% 344 2.3 3.1 3.1 23.1 0.06. (waiting for reply)7. 193.149.1.97 0.0% 344 3.6 3.6 3.6 38.6 0.08. (waiting for reply)9. 185.125.78.197 2.9% 344 6.9 6.9 6.9 6.9 0.0
As you can see in hop 5, almost 50% of the packets are lost, which would help me call my phone company and ask them to try routing me through a different path.
Name of our machine with hostname
If we want to know the name of our machine, we can use hostname
, which is useful if we want to connect to our machine from another one.
!hostname
wallabot
Default gateway information with route -n
If we want to know our default gateway we use the command route -n
!route -n
Tabla de rutas IP del núcleoDestino Pasarela Genmask Indic Métric Ref Uso Interfaz0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 enp6s0169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 enp6s0172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-470e52ae2708192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 enp6s0
IP Information of a Domain with nslookup
If we want to know the IP of a domain, we can find it out using the nslookup
command.
!nslookup google.com
Server: 127.0.0.53Address: 127.0.0.53#53Non-authoritative answer:Name: google.comAddress: 142.250.185.14Name: google.comAddress: 2a00:1450:4003:808::200e
This tells us that Google's IPv4 is 172.217.168.174 and its IPv6 is 2a00:1450:4003:803::200e
Information about our network with netstats
The last utility command is netstats
, this command gives us the status of our network, and with the -i
flag it returns our network interfaces.
!netstat -i
Tabla de la interfaz del núcleoIface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flgbr-470e5 1500 0 0 0 0 0 0 0 0 BMUdocker0 1500 0 0 0 0 0 0 0 0 BMUenp6s0 1500 148385 0 2182 0 106135 0 0 0 BMRUlo 65536 11674 0 0 0 11674 0 0 0 LRUwlp5s0 1500 0 0 0 0 0 0 0 0 BMU
DNS Queries with dig
With the command dig <domain>
we can make DNS queries, for example, let's make a query to Google
!dig google.com
; <<>> DiG 9.16.1-Ubuntu <<>> google.com;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20527;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 65494;; QUESTION SECTION:;google.com. IN A;; ANSWER SECTION:google.com. 283 IN A 142.250.184.14;; Query time: 8 msec;; SERVER: 127.0.0.53#53(127.0.0.53);; WHEN: dom sep 24 01:32:07 CEST 2023;; MSG SIZE rcvd: 55
It can be seen
```;; ANSWER SECTION:google.com. 283 IN A 142.250.184.14```
Therefore, the query has given us Google's IP
We can query a particular DNS server with dig @<DNS server> <domain>
!dig @1.1.1.1 google.com
; <<>> DiG 9.16.1-Ubuntu <<>> @1.1.1.1 google.com; (1 server found);; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15633;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0;; QUESTION SECTION:;google.com. IN A;; ANSWER SECTION:google.com. 190 IN A 142.250.184.14;; Query time: 8 msec;; SERVER: 1.1.1.1#53(1.1.1.1);; WHEN: dom sep 24 01:33:40 CEST 2023;; MSG SIZE rcvd: 44
We have made the same query, but we have made it to Cloudflare's DNS.
Compressing Files
Before compressing and decompressing, let's see what we are going to compress. First, we print our path and list the files.
!pwd; ls -l
/home/wallabot/Documentos/web/portafolio/posts/pruebatotal 332-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 15131 dic 6 01:49 google2.html-rw-rw-r-- 1 wallabot wallabot 15168 dic 6 01:48 google.html-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 15117 dic 6 01:49 index.html.1-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txt
Let's create a new folder and copy everything that is inside the current folder into it.
!mkdir tocompress; cp * tocompress; ls -l
cp: -r not specified; omitting directory 'tocompress'total 336-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 15131 dic 6 01:49 google2.html-rw-rw-r-- 1 wallabot wallabot 15168 dic 6 01:48 google.html-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 index.html-rw-rw-r-- 1 wallabot wallabot 15117 dic 6 01:49 index.html.1-rw-rw-r-- 1 wallabot wallabot 182 dic 6 01:06 lista.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 01:08 secuential.txtdrwxrwxr-x 2 wallabot wallabot 4096 dic 6 01:52 tocompress
As we can see, everything except the tocompress
folder itself has been copied because we did not use the -r
flag in the cp
command. But what happened is what we wanted.
Compress with tar
The first command we are going to use to compress is tar
to which we will add the -c
flag for compress, -v
for verbose, so it will show us what it is doing, and the -f
flag for file, followed by the name we want for the compressed file and the name of the file we want to compress.
!tar -cvf tocompress.tar tocompress
tocompress/tocompress/lista.txttocompress/dot.txttocompress/google.htmltocompress/index.htmltocompress/Abctocompress/google2.htmltocompress/dot2.txttocompress/secuential.txttocompress/index.html.1tocompress/file.txttocompress/datos1tocompress/2021-02-11-Introduccion-a-Python.ipynbtocompress/datos123
!ls -l
total 676-rw-rw-r-- 1 wallabot wallabot 285898 dic 6 00:28 2021-02-11-Introduccion-a-Python.ipynb-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 Abc-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos1-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 datos123-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot2.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 dot.txt-rw-rw-r-- 1 wallabot wallabot 0 dic 6 00:56 file.txt-rw-rw-r-- 1 wallabot wallabot 15131 dic 6 01:49 google2.html-rw-rw-r-- 1 wallabot wallabot 15168 dic 6 01:48 google.html