Linux Commands

AT - Schedule Commands

The AT command can be used to schedule one-off commands. To schedule repetative commands you can use Cron. For more info on Cron click here.

Install AT

sudo apt-get update

sudo apt-get install at

Scheduling Examples

Taken from https://tecadmin.net/one-time-task-scheduling-using-at-commad-in-linux/.

Create a job that will be executed at 9:00 am:

at 09:00

Schedule a job for the coming Sunday at a time ten minutes later than the current time:

at sunday +10 minutes

Schedule a job to run at 1pm two days from now:

at 1pm + 2 days

Schedule a job to run at 12:30 Oct 21 2020:

at 12:30 102120

Schedule a job to run one hour from now:

at now +1 hours

Run GAM Command

To run a GAM command, start with the schedule time option, for example, to run a command in 1 minute, type at now + 1 minute press enter and after at>  type the gam command, including the full path to the gam folder and press enter. Then press enter then CTRL + d to escape.

$ at now + 1 minute

warning: commands will be executed using /bin/sh

at> /home/user_name/bin/gamadv-xtd3/gam print cros todrive

at> <EOT>

job 8 at Fri Oct 23 08:32:00 2020

You have new mail in /var/mail/user_name

$ 

Your GAM command will run as scheduled.

Mail

A log of the command and any errors is saved in the /var/mail/user_name file. To view this use nano /var/mail/user_name.

Once you are confident that the command will run as required, you cab use the -M switch to prevent entries being added to the mail file. So the initial command will be;-

$ at now + 1 minute -M

Pipe Command

Commands can also be piped | into the at command. For example;-

echo "/home/user_name/bin/gamadv-xtd3/gam print cros todrive" | at now + 1 minute -M

Running Scripts

The at command can also be used to run script files. Click here for more info in creating script files. The. run the script in a similar way.

echo "./your-file-name.sh" | at now + 1 minute

View & Delete

To view current jobs use the atq command. This will list the current jobs, each line starts with their job number.

To remove a job use atrm <Job Number>.

CD - Change Directory

cd <folder name>

Go to the parent directory

cd ..

Go to Home Directory

cd /

CP - Copy

cp ExistingFileName NewFileName

LS - List command

File and folder names are case sensitive.

Show Files & Folders

ls

List

ls -l

View Hidden Files

ls -a

List and Orderby Date

ls -lt

List and Orderby Size

ls -lS

Output a list of a certain file type

ls *.csv

Create a TXT file listing the file names of a certain type of file.

ls *.csv > <File Name>.txt

MV - Rename or Move files

Note, if your file name has certain characters, such as brackets (happens if you upload a file with the same name as one that already exists), then you need to enclose the file name in ''. You may need to precede the command with sudo.

mv <original_file_name> <new_file_name>

mv <original_file_name> <directory>

mv /home/user/old-directory /home/user/new-directory

Nano - Edit Files

Edit a file from the command line (terminal)

nano <File Name>

Exit - CTRL + X and follow oncreen prompt for saving changes and file name.

Wrap lines - ESC followed by SHIFT + 4

PWD - File Path

In the Cloud you sometimes need to specify the exact file path, rather than the relative path, e.g. for downloading a specific file. To find the path of the current location type

pwd

RM - Remove/Delete

rm <File Name>

rm -R <Directory Name>

Remove all CSV files in current directory

rm *.csv

SCREEN

Use the screen command to create SSH sessions that will remain active after the terminal window is closed.

Start Screen

screen

Detach

Enter the following key command Ctrl + a + d. This allows the terminal session to remain active, when you close the terminal window.

List running sessions

This will show the session(s) running, e.g. 7849.pts-0.<Compute Engine Name>. This can be run from the main terminal window (i.e. you do not need to be logged into a screen session). This session number will also be shown when you detach a session.

screen -ls

Re-attach a session

If only 1 session is running, then screen -r will re-attach the session, otherwise;-

screen -r 7840

Close a Screen session

Exit will close the current screen session and return to the main terminal session. Using exit will prevent you from reconnecting to this session.

exit

Kill a Screen Session

screen -X -S <Session #> quit

More info from

https://www.tecmint.com/screen-command-examples-to-manage-linux-terminals/

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-screen-on-an-ubuntu-cloud-server

tAIL - display lines from a file

The tail command can be used to display lines from a file. In this example, it is used to create a variable for the URL of a file created when using the todrive option. This can then be emailed to a user.

gam redirect stdout ./output print cros todrive && url=`tail -n 1 output` && gam user <User Email Address> sendemail recipient <User Email Address> subject "<Subject Text>" message "$url"

Breakdown

gam redirect stdout ./output print cros todrive - The GAM command with the todrive option that will output to the file called output.

&& url=`tail -n 1 output` - Once the GAM command has completed, a variable called url is created using the second line of the file.

&& gam user <User Email Address> sendemail recipient <User Email Address> subject "<Subject Text>" message "$url" - the sendmail command will send an email with the message of just the URL of the file created.

MORE - View Files

more <File Name> - use the space bar to scroll through the document

Zip

Install

sudo apt-get install zip

Create

Create a Zip file of the files in a folder

zip -r <File Name>.zip <Folder Name>

Add multiple files to a Zip

zip -r <File Name>.zip <File Name 1> <File Name 2> <File Name 3>

Extract tar.xz

tar -xf file.tar.xz

CTRL commandS

CTRL + c - stop the command running

CTRL + a - go to the start of the line

CTRL + e - go to the end of the line

CTRL + d - delete

CTRL + r - search command history. When you see (reverse-i-search) start typing part of the previous command.

CTRL + p - scroll through previous commands

Pipes |

If you are on Linux or Mac OS you can avoid creating a CSV file and use pipes to pipe the output of one GAM command directly into another like this:

This example will Delete a specific File Type for a user.

gam user <user email address> print filelist query "mimeType='audio/mp3'" id title mimetype | gam csv - gam user ~Owner delete drivefile ~id purge

For more check out https://www.howtogeek.com/438882/how-to-use-pipes-on-linux/ or search the web for other tutorials