Thursday, 19 June 2014

Basic Shell Things

These are related to deployment scripts in Virtual Fly Brain.

Find files

Use the simple find command:

find *.txt

finds all files ending in .txt in current directory (note that the * is the wildcard for any bumber of any characters (including none at all).

find -name '*.txt'

finds all files ending in .txt in this and below directories

find data/ -name '*.txt'

finds all files ending in .txt in directory data

find -name '*.txt' -or -name '*.htm'

find files ending in either .txt or .htm


If - else - fi format example

if [ ! -d datafolder ]
then
      echo "Data folder NOT present"
else
      echo "Data folder present"
fi

-d for directory existence check, -f for files.
note that logical operators greater than can be represented as -gt and less than as -lt (less than or equal to as -le and greater than or equal to as -ge)


Output to a file

command > filename.txt e.g.

echo "ddd" > 3ds.txt

Output to the screen

Simply just display a file on the screen e.g. when outputting variables to it...

cat 3ds.txt



Output as the input to another command

Simply run the command on the data from the first all in one line as:

cat 3ds.txt | sed s/d/e/g

Here outputs "eee" to the screen.



Output the result of a command as arguments in another command

E.g. when you want to pass a filename instead of the actual output text use xargs instead of just the line "|" (pipe)

find 3ds* | xargs sed s/d/e/g | cat

Here since find just returns the filename without xargs we simply get 3es.txt.  We even need to use xargs to view the file as:

find 3ds* | xargs cat



Sed (uses usually in-built sed program)

sed s/day/night/g input.txt > output.txt

replaces "day" with "night" when writing input file to new output file, g makes sure it is applied to all instances not just first.

or do the edits in-place:

sed -i s/day/night/ input.txt

We can also pass entire scripts to sed to run using the -f command:

sed -i input.txt -f script.sed



Get lines containing text string

grep searchterm textfile.txt

Simply returns the whole lines containing the searchterm


Simply cut text, use cut command

cut -c -10 textfile.txt


Execute a shell script using nice for prioritisation:

nice script.sh

must be executable (chmod +x script.sh)


Print the number of lines in a file

wc -l textfile.txt

also -c for bytes count and -m for character count

No comments:

Post a Comment