Bash

Aus robopagex.com
Zur Navigation springen Zur Suche springen

Script

$ type script
$ chmod +x script
$ ./script
$ .script
$ source script
$ bash script
#!/bin/bash

Command Line

Everything is a file
255 Byte
The maximum length for a file name is 255 bytes.
The maximum combined length of both the file name and path name is 4096 bytes.
This length matches the PATH_MAX that is supported by the operating system.
Names are case-sensitive

Special chars to avoid in names/filenames

/   Never
\   Escaped
-   Never at beginning
[]  Escaped
{}  Escaped
*   Escaped
'   Escaped
"   Escaped

Wildcards

*  asterisk        | any char
?  question mark   | single char
[] square brackets | set of single char or a range of chars

List files and folders

$ ls
$ ls folder
$ ls ./folder
$ ls /path/folder
$ ls ~/folder
$ ls ~/folder/*.jpg
$ ls ~/folder/*txt*
$ ls -R folder (list of subfolders)
$ ls -l (single column)
 - regular file
 _ executable
 d directory
 l symbolic link
 s socket
 b block device
 c character device
 p named pipe
$ ls -m (comma-separated list)
$ ls -a (hidden files and folders)
$ ls -F (file type)
Symbols and file types
 * Executable
 / Directory
 @ Symbolic link
 | FIFO
 = Socket
$ ls --color (content in color)
$ dircolors
$ dircolors --print-database
$ ls -F --color
$ la -la
$ ls -r
$ ls -X (sort by extension)
$ ls -t (sort by date)
$ ls -S (sort by content)
$ ls -h
$ ls -laS
$ ls -h
$ pwd (current path)

Change directory with cd

$ cd folder
$ cd ~
$ cd -
$ cd ..

Create and change current time of file touch

create new, empty file
$ touch file
update access and modification time for file
$ touch file
any desired time for file
$ touch -t [[CC]YY]MMDDhhmm[.ss]file

Create directory with mkdir

new directory
$ mkdir folder
create directory with subdirectories
$ mkdir -p test/sub/folder
create directory and show steps
$ mkdir -v test/sub/folder
$ mkdir -pv test/sub/folder

Count files and directories, summary size and EXCLUDE folders

bash count files and directory, summary size and EXCLUDE folders that are fuse|sshfs

Full size of a folder with du

get the full size of workdir | no fuse/sshfs in use
$ du -hs workdir
get the full size of workdir, excluding mysshfs | fuse/sshfs in use
$ du -hs --exclude=mysshf workdir

Count files in folder with find

count files in workdir | no fuse/sshfs in use
$ find workdir -type f | wc -l
count files in workdir, excluding mysshfs | no fuse/sshfs in use
$ find workdir -type f -not -path "*mysshfs*" | wc -l
count files in workdir, excluding mysshfs | fuse/sshfs in use
$ find workdir -path "*/mysshfs/*" -prune -o \( -type f -print \) | wc -l

Count folders in folder with find

count folders in workdir | no fuse/sshfs in use
$ find workdir -type d | wc -l
count folders in workdir, excluding mysshfs | no fuse/sshfs in use
$ find workdir -type d -not -path "*mysshfs*" | wc -l
count folders in workdir, excluding mysshfs | fuse/sshfs in use
$ find workdir -path "*/mysshfs/*" -prune \( -type d -print \)  | wc -l

Building blocks

run several commands sequentially with ; and &&

executed sequential no matter if successfully or unsucessfully
;
$ ls /home ; ls notfound; ls ~
executed sequential if successfully run next
&&
$ ls /home && ls notfound && ls ~

PORT SCAN with bash's built-in /dev/tcp

check if host response on a give port with bash's built-in /dev/tcp

#!/bin/bash
HOST_NAME="127.1"
HOST_PORT="80"
if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
   echo -e "PORT: ${HOST_PORT} | ON"
   else
   echo -e "PORT: ${HOST_PORT} | OF"
fi
exit; 
#!/bin/bash
HOST_NAME="127.1"
for HOST_PORT in {1..1000}
do
if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
   echo -e "PORT: ${HOST_PORT} | ON"
   else
   echo -e "PORT: ${HOST_PORT} | OF"
fi
done
exit;
#!/bin/bash
HOST_NAME="127.1"
declare -A PORT_ON
for HOST_PORT in {1..65535}
do
   if ( (exec 3<>/dev/tcp/${HOST_NAME}/${HOST_PORT}) 2> /dev/null); then
       PORT_ON[${HOST_PORT}]="ON"
   fi
done
for i in ${!PORT_ON[*]}
do
   echo -e "$i : ${PORT_ON[$i]}"
done
exit;