Skip to content

Instantly share code, notes, and snippets.

@igarag
Last active February 22, 2024 08:47
Show Gist options
  • Save igarag/f5238b30a2670d37758c466cc620daa3 to your computer and use it in GitHub Desktop.
Save igarag/f5238b30a2670d37758c466cc620daa3 to your computer and use it in GitHub Desktop.
Comandos en Linux para obtener información del sistema.

Linux - cheat sheets

Comandos generales

Descripción Comando
Eliminar ficheros por fecha (todos los que superen la fecha) find /opt/backup -type f -mtime +30 -exec rm -f {} \;
Eliminar ficheros por nombre find /var/log -name "*.log" -type f -mtime +30 -exec rm -f {} \;
Calcular y ordenar directorios por tamaño sudo du -sch [!.]* *
Detectar máquinas con nmap nmap -sn 192.168.0.1-254 ó nmap -v -sn 192.168.0.0/24
Ver puertos y estados netstat -plunt
Ver puertos activos en la propia máquina netstat -lantp
Listar servicios de Linux service --status-all
Ver tamaño de la carpeta (UNIX) du -sh file_path
rograma para ver la lista de repositorios ppa y -ppa-manager
Ver lista de dispositivos conectados xinput --list -short
Obtener PID de un servicio `ps aux
Ver y matar procesos en segundo plano jobs (ver procesos en bg) --> kill %[NUMERO_PROCESO] # Ej: kill %2
Servidor simple de Python python -m SimpleHTTPServer [PORT]

Buscar en Linux con find

Comando Descripción
sudo find /home/nombre_usuario/Escritorio -name “\*.jpg” Busca todos los archivos del Escritorio con extensión .jpg
sudo find . -name “\*hola\*” Busca todos los archivos que contienen la palabra “hola” en el nombre.
sudo find . ! -name “\*hola\*” Busca todos los archivos que NO contienen la palabra “hola” en el nombre
sudo find . -name “\*hola\*” -a “\*caracola\*” Busca todos los archivos que contienen la palabra “hola” y “caracola” en el nombre.
sudo find . -name “\*hola\*” -o “\*caracola\*” Busca todos los archivos que contienen la palabra “hola” o “caracola” en el nombre.
sudo find . -iname “\*hola\*” Busca todos los archivos que contienen la palabra “hola” en el nombre tanto en mayúsculas como en minúsculas.
sudo find . -user nombre_usuario Busca todos los archivos de un usuario determinado (nombre_usuario).
sudo find . -size +1000k Busca todos los archivos de tamaño mayor a 1000 kb.
sudo find . -amin -30 Busca todos los archivos a los que se accedió en los últimos 30 minutos.
sudo find . -atime 365 Busca todos los archivos a los que se accedió hace un año exactamente.
sudo find . -name “\*.jpg” -exec rm {} \; Busca todos los archivos con extensión .jpg y los borra.

Buscar en Linux con locate

Comando Descripción
sudo locate “\*.jpg” Busca todos los archivos con extensión .jpg.
sudo locate hola Busca todos los archivos que contienen la palabra “hola” en el nombre.
sudo locate -c hola Muestra cuantos archivos contienen la palabra “hola” en el nombre.
sudo locate -e hola Busca todos los archivos que contienen la palabra “hola” en el nombre y que no han sido borrados previamente.
sudo locate -i hola Busca todos los archivos que contienen la palabra “hola” en el nombre tanto en mayúsculas como en minúsculas.

SCP

Description Command
Copy a file from remote to local host scp username@remotehost:/path/file /local/host
Copy the file from localhost to remote host scp file username@remotehost:/path/
Copy the directory from localhost to a remote host scp -r dir username@remotehost:/path/
Copy the file from remote host edu1 to remote host edu2 scp username@remotehos1.edu1:/path/file username2@remotehost2.edu2:/path/
Copy 2 files from local host to remote scp file1 file2 username@remotehost:/path/

Descompresión

.tgz tar -xvzf file.tgz .tar tar -xvf file.tar
.bz2 bzip2 -d file.bz2 .tar.bz2 tar jxvf file.tar.bz2
.gz gzip -d file.gz .tar.gz tar zxf file.tar.gz
.rar rar x file.rar .lha lha x file.lha
.zip unzip file.zip .zoo zoo x file.zoo

Compresión Fuerte

GZIP=-9 tar -czvf fichero.tar.gz

Format disk comman-line

Umount the disk

sudo umount /dev/sdb1
  • Format with EXT4 File System (linux)

    sudo mkfs.ext4 /dev/sdc1
  • Format with vFat File System

    sudo mkfs.vfat /dev/sdc1
  • Format with NTFS File System

    sudo mkfs.ntfs /dev/sdc1

Mount drive in linux and set auto-mount at boot

Skip to end of metadata Guide to mount a drive in linux (deb/ubuntu) and set to auto-mount at boot.

Mount drive

Make a folder (will be mount point)

sudo mkdir /media/data
sudo mount /dev/sdb1 /media/data

Now you can access the drive at /media/data.

Auto-mount at boot

We want the drive to auto-mount at boot. This usually means editing /etc/fstab.

Firstly, it's always best to use the drives UUID. To find the drive's UUID do:

ls -al /dev/disk/by-uuid/

Copy the resultant UUID (for your disk) and then open fstab for editing:

sudo vim /etc/fstab

You want to add an entry for the UUID and mount point. Below is an example of an fstab file with an entry added for the mount above:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb1 during installation
UUID=63a46dce-b895-4c1f-9034-b1104694a956 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sdb5 during installation
UUID=b9b9ee49-c69c-475b-894b-1279d44034ae none            swap    sw              0       0
# data drive
UUID=19fa40a3-fd17-412f-9063-a29ca0e75f93 /media/data   ext4    defaults        0       0

Test fstab

We always want to test the fstab before rebooting (an incorrect fstab can render a disk unbootable). To test do:

sudo mount -a

If nothing is returned (e.g. no errors) then you should be good to go.

Unmounting drive with umount

You can unmount drives using umount. For example, to unmount the data drive above mount at /media/data you would do:

sudo umount /media/data

Eliminar kernels antiguos

Con el siguiente comando podemos listarlos:

dpkg --list | egrep -i --color 'linux-image | linux-headers'

donde las que empiezan por rc son los que no están en uso o están desinstalados y los ii los que están instalados en el sistema. Un ejemplo de la salida puede ser el siguiente:

ii  linux-headers-5.3.0-62          5.3.0-62.56~18.04.1     all          Header files related to Linux kernel version 5.3.0
ii  linux-headers-5.3.0-62-generic  5.3.0-62.56~18.04.1     amd64        Linux kernel headers for version 5.3.0 on 64 bit x86 SMP
ii  linux-headers-5.4.0-42-generic  5.4.0-42.46~18.04.1     amd64        Linux kernel headers for version 5.4.0 on 64 bit x86 SMP
ii  linux-headers-generic-hwe-18.04 5.4.0.42.46~18.04.35    amd64        Generic Linux kernel headers
rc  linux-image-4.18.0-15-generic   4.18.0-15.16~18.04.1    amd64        Signed kernel image generic
rc  linux-image-5.3.0-42-generic    5.3.0-42.34~18.04.1     amd64        Signed kernel image generic
rc  linux-image-5.3.0-45-generic    5.3.0-45.37~18.04.1     amd64        Signed kernel image generic
rc  linux-image-5.3.0-46-generic    5.3.0-46.38~18.04.1     amd64        Signed kernel image generic
rc  linux-image-5.3.0-51-generic    5.3.0-51.44~18.04.2     amd64        Signed kernel image generic
rc  linux-image-5.3.0-53-generic    5.3.0-53.47~18.04.1     amd64        Signed kernel image generic
rc  linux-image-5.3.0-59-generic    5.3.0-59.53~18.04.1     amd64        Signed kernel image generic
rc  linux-image-5.3.0-61-generic    5.3.0-61.55~18.04.1     amd64        Signed kernel image generic
ii  linux-image-5.3.0-62-generic    5.3.0-62.56~18.04.1     amd64        Signed kernel image generic
ii  linux-image-5.4.0-42-generic    5.4.0-42.46~18.04.1     amd64        Signed kernel image generic
ii  linux-image-generic-hwe-18.04   5.4.0.42.46~18.04.35    amd64        Generic Linux kernel image

Para seleccionar aquellos que no están en uso:

x=$(dpkg --list | grep -i linux-image | grep ^rc | awk '{print $2}')

Ver los elementos a borrar con:

echo "$x"

Ejemplo de salida con la entrada anterior:

linux-image-4.18.0-15-generic
linux-image-5.3.0-42-generic
linux-image-5.3.0-45-generic
linux-image-5.3.0-46-generic
linux-image-5.3.0-51-generic
linux-image-5.3.0-53-generic
linux-image-5.3.0-59-generic
linux-image-5.3.0-61-generic

Eliminar definitivamente con:

sudo apt --purge remove $x

Bloquear actualizaciones de paquetes en Ubuntu

Holding There are four ways of holding back packages: with dpkg, apt, aptitude or dselect.

dpkg

Put a package on hold:

echo "<package-name> hold" | sudo dpkg --set-selections

Remove the hold:

echo "<package-name> install" | sudo dpkg --set-selections

Display the status of your packages:

dpkg --get-selections

Display the status of a single package:

dpkg --get-selections | grep "<package-name>"

apt

Hold a package:

sudo apt-mark hold <package-name>

Remove the hold:

sudo apt-mark unhold <package-name>

Show all packages on hold:

sudo apt-mark showhold

Enable/Disable change desktop in all screens

gsettings set org.gnome.shell.overrides workspaces-only-on-primary false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment