Using the UNIX/Linux sleep command to pause a shell script or command for a short time. And other useful commands.

Posted: May 31, 2012. At: 3:05 PM. This was 5 years ago. Post ID: 3972

The Linux sleep command will enable you to pause a Linux command for a short time. The example below has our command halting for 5 seconds before executing. The suffix s means seconds, this is useful for halting a script whilst you are waiting for something else to run.

C:\HOME\FLYNN\DESKTOP> sleep 5s ; ps
  PID TTY          TIME CMD
11113 pts/3    00:00:00 bash
30712 pts/3    00:00:00 ps

The time command is used to time a command and see how long the command takes to execute. This can be useful for seeing how much faster a program is with any optimizations you are performing on it.

C:\HOME\FLYNN\DESKTOP> time ls
total 5.4M
drwxr-xr-x  3 flynn flynn 4.0K  31-05-12 01:07 pm .
drwxr-xr-x 43 flynn flynn 4.0K  31-05-12 01:27 pm ..
-rw-r--r--  1 flynn flynn 207K  27-05-12 12:25 pm cloudflare.png
-rw-r--r--  1 flynn flynn   13  31-05-12 02:48 pm phonenumber.txt
drwxr-xr-x  2 flynn flynn 4.0K  29-05-12 04:31 pm Police_code_files
-rw-r--r--  1 flynn flynn 144K  31-05-12 02:48 pm Police_code.html
-rw-r--r--  1 flynn flynn  73K  29-05-12 06:18 pm powericon.jpg
-rw-r--r--  1 flynn flynn 650K  30-05-12 05:32 am riverlight.jpg
-rw-r--r--  1 flynn flynn  85K  29-05-12 06:42 pm screenshot1.png
-rw-r--r--  1 flynn flynn  87K  29-05-12 06:42 pm screenshot2.png
-rw-r--r--  1 flynn flynn 107K  29-05-12 06:43 pm screenshot3.png
-rw-r--r--  1 flynn flynn 125K  29-05-12 07:01 pm screenshot4.png
-rw-r--r--  1 flynn flynn  76K  29-05-12 07:05 pm screenshot5.png
-rw-r--r--  1 flynn flynn 1.7K  29-05-12 07:28 pm screenshot6.png
-rw-r--r--  1 flynn flynn 150K  29-05-12 07:31 pm screenshot7.png
-rw-r--r--  1 flynn flynn  40K  30-05-12 04:06 am screenshot8.png
-rw-r--r--  1 flynn flynn 2.2M  31-05-12 01:07 pm screenshot9.png
-rw-r--r--  1 flynn flynn  34K  26-05-12 11:37 pm Screenshot-About MATE Desktop Environment.png
-rw-r--r--  1 flynn flynn 1.2M  29-05-12 06:17 pm Screenshot.png
-rw-r--r--  1 flynn flynn 225K  29-05-12 03:51 pm Screenshot-Synaptic Package Manager .png
 
real	0m0.005s
user	0m0.000s
sys	0m0.004s

The xxd command is used to perform a hexdump of a certain file. This example below is me converting a text format file to a hexdump. Then you may use the xxd -r profile.hex > profile to convert the hexdump back into the original file.

C:\HOME\FLYNN\DESKTOP> xxd profile > profile.hex
C:\HOME\FLYNN\DESKTOP> cat profile.hex
0000000: 2320 2f65 7463 2f70 726f 6669 6c65 3a20  # /etc/profile: 
0000010: 7379 7374 656d 2d77 6964 6520 2e70 726f  system-wide .pro
0000020: 6669 6c65 2066 696c 6520 666f 7220 7468  file file for th
0000030: 6520 426f 7572 6e65 2073 6865 6c6c 2028  e Bourne shell (
0000040: 7368 2831 2929 0a23 2061 6e64 2042 6f75  sh(1)).# and Bou
0000050: 726e 6520 636f 6d70 6174 6962 6c65 2073  rne compatible s
0000060: 6865 6c6c 7320 2862 6173 6828 3129 2c20  hells (bash(1), 
0000070: 6b73 6828 3129 2c20 6173 6828 3129 2c20  ksh(1), ash(1), 
0000080: 2e2e 2e29 2e0a 0a69 6620 5b20 2224 5053  ...)...if [ "$PS
0000090: 3122 205d 3b20 7468 656e 0a20 2069 6620  1" ]; then.  if 
00000a0: 5b20 2224 4241 5348 2220 5d20 2626 205b  [ "$BASH" ] && [
00000b0: 2022 2442 4153 4822 2021 3d20 222f 6269   "$BASH" != "/bi
00000c0: 6e2f 7368 2220 5d3b 2074 6865 6e0a 2020  n/sh" ]; then.  
00000d0: 2020 2320 5468 6520 6669 6c65 2062 6173    # The file bas
00000e0: 682e 6261 7368 7263 2061 6c72 6561 6479  h.bashrc already
00000f0: 2073 6574 7320 7468 6520 6465 6661 756c   sets the defaul
0000100: 7420 5053 312e 0a20 2020 2023 2050 5331  t PS1..    # PS1
0000110: 3d27 5c68 3a5c 775c 2420 270a 2020 2020  ='\h:\w\$ '.    
0000120: 6966 205b 202d 6620 2f65 7463 2f62 6173  if [ -f /etc/bas
0000130: 682e 6261 7368 7263 205d 3b20 7468 656e  h.bashrc ]; then
0000140: 0a20 2020 2020 202e 202f 6574 632f 6261  .      . /etc/ba
0000150: 7368 2e62 6173 6872 630a 2020 2020 6669  sh.bashrc.    fi
0000160: 0a20 2065 6c73 650a 2020 2020 6966 205b  .  else.    if [
0000170: 2022 6069 6420 2d75 6022 202d 6571 2030   "`id -u`" -eq 0
0000180: 205d 3b20 7468 656e 0a20 2020 2020 2050   ]; then.      P
0000190: 5331 3d27 2320 270a 2020 2020 656c 7365  S1='# '.    else
00001a0: 0a20 2020 2020 2050 5331 3d27 2420 270a  .      PS1='$ '.
00001b0: 2020 2020 6669 0a20 2066 690a 6669 0a0a      fi.  fi.fi..
00001c0: 2320 5468 6520 6465 6661 756c 7420 756d  # The default um
00001d0: 6173 6b20 6973 206e 6f77 2068 616e 646c  ask is now handl
00001e0: 6564 2062 7920 7061 6d5f 756d 6173 6b2e  ed by pam_umask.
00001f0: 0a23 2053 6565 2070 616d 5f75 6d61 736b  .# See pam_umask
0000200: 2838 2920 616e 6420 2f65 7463 2f6c 6f67  (8) and /etc/log
0000210: 696e 2e64 6566 732e 0a0a 6966 205b 202d  in.defs...if [ -
0000220: 6420 2f65 7463 2f70 726f 6669 6c65 2e64  d /etc/profile.d
0000230: 205d 3b20 7468 656e 0a20 2066 6f72 2069   ]; then.  for i
0000240: 2069 6e20 2f65 7463 2f70 726f 6669 6c65   in /etc/profile
0000250: 2e64 2f2a 2e73 683b 2064 6f0a 2020 2020  .d/*.sh; do.    
0000260: 6966 205b 202d 7220 2469 205d 3b20 7468  if [ -r $i ]; th
0000270: 656e 0a20 2020 2020 202e 2024 690a 2020  en.      . $i.  
0000280: 2020 6669 0a20 2064 6f6e 650a 2020 756e    fi.  done.  un
0000290: 7365 7420 690a 6669 0a                   set i.fi.
C:\HOME\FLYNN\DESKTOP> xxd -r profile.hex > profile

For any of these commands you may use the info command to get extra help, e,g info sleep will print a page of information with all of the command line switches.

No comments have been made. Use this form to start the conversation :)

Leave a Reply