Tag Archives: Linux - Page 4

Remove files in batches of x files

Every now and then I try to do things on a large amount of files – this often ends in a command error about “Too many files” or something similar. One solution to this is to send batches of information to the command instead of sending all at once.

Here is an example on how to delete files in batches of 1000 files:

ls | xargs -n 1000 rm -v

The trick here is the -n 1000 that will send the result from the ls command in batches of a 1000 results per time until the ls list is empty to rm. Neat ah? 🙂

Tested on RHEL 3

Repeat a command every x seconds

Say you wanted to see whenever the content of a folder changes – what to do? Easy! Use the watch command:

watch 'ls -l'

This will clear the screen and output the contents of the command and update it every 2 seconds (clearing screen before each update). The watch command works for most scripts and commands

Tip: Using Tmux and the watch command together can help you create a really cool window with a bunch of “live” updated small windows

Tested on RHEL 3

Create a SSH tunnel inside a chain of SSH tunnels example

Here is an example of SSH tunnel setup inside a couple of SSH tunnels to create an all encrypted connection.

The setup is shown in the picture below:
System setup
The following rules apply:

  • Server 1 is public
  • Server 2 can only be accessed through Server 1
  • Server 3 can only be accessed through Server 2
  • All Servers have a SSH server running
  • My computer has only an SSH client

To set this up we start with the “small” tunnels:

Step 1

Set up Tunnel 3 with the following command:

ssh -N -L 2000:localhost:22 user@Server3

This creates a persitent (-N option) port forwarding from port 22 on Server 3 and to port 2000 on Server 2

Step 2

Set up Tunnel 2 between Server 1 and Server 2 with this command:

ssh -N -L 3000:localhost:2000 user@Server2

This sets upp a persistent tunnel between port 2000 on Server 2 and to port 3000 on Server 1

Step 3

Set up my connection (Tunnel 1) to the tunnel on Server 1 (the tunnel which is going all the way to Server 3)

ssh -N -L 4000:localhost:3000 user@Server1

This sets up a persistent tunnel between port 3000 on Server 1 and to port 4000 on My Computer

All the “small” tunnels are done!

Now we create a tunnel inside these tunnels to keep the connection secure:

ssh -p 4000 -N -L 8080:localhost:80 -o HostKeyAlias="Server3" user@localhost

This creates a tunnel from port 80 on Server 3 to port 8080 on My Computer. Lets take a closer look:

  • -p 4000 – we start a new tunnel by connecting to port 4000 on My Computer (see port 4000 in last step above). This means that we are connecting through the tunnel we setup to Server 1 (that is connected to Server 2 – that is connected to Server 3)
  • 8080:localhost:80 – we are forwarding port 80 on Server 3 to port 8080 on My Computer
  • HostKeyAlias=”Server3″ – this is often needed since the new tunnel should authenticate with Server3 and not My Computer. Failing to provide this may lead to “Man-in-the-Middle” attack warnings and failure to log on Server 3
  • user@localhost – localhost in this case is actually Server 3 so use your Server 3 credentials here

To add more tunnels just use the last command and define new ports to forward. As soon as the “small” tunnels are setup you can reach any open port on Server 3 for a tunnel

Not easy but now at least I have something to start with 🙂