Difference between revisions of "Console Command Examples"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(Created page with "Linux specific console command examples Examples from other pages included. References also included. This document is in the form of "what you want to do" and "how to do i...")
 
m (change file case UPPERCASE to lowercase in the same directory)
 
(4 intermediate revisions by one user not shown)
Line 2: Line 2:
  
 
Examples from other pages included.  References also included.  This document is in the form of "what you want to do" and "how to do it" taken from our old Linux forum with questions from linux users.
 
Examples from other pages included.  References also included.  This document is in the form of "what you want to do" and "how to do it" taken from our old Linux forum with questions from linux users.
 +
 +
There are many very good and useful examples in our old [[Advanced Shell Operations]].
  
 
===change file case UPPERCASE to lowercase in the same directory===
 
===change file case UPPERCASE to lowercase in the same directory===
Line 12: Line 14:
 
  do                                  # uses the tr command to convert case
 
  do                                  # uses the tr command to convert case
 
   mv -i "$FILE" `echo "$FILE" | tr '[A-Z]' '[a-z]'` 2> /dev/null
 
   mv -i "$FILE" `echo "$FILE" | tr '[A-Z]' '[a-z]'` 2> /dev/null
  done  
+
  done
 +
 
 +
These may fail on unsupported file systems such as FAT32 with an error such as:
 +
mv: 'IMAG0001.JPG' and 'imag0001.jpg' are the same file
 +
./IMAG0004.JPG not renamed: ./imag0004.jpg already exists
 +
In this case you will have to move when you change case and move back again.  Here we will change an uppercase file extension to a lowercase one under FAT32
 +
rename 's/\.JPG$/\.jpeg/' *.JPG
 +
rename 's/\.jpeg$/\.jpg/' *.jpeg
 +
 
 +
There are plenty of other ways to accomplish such tasks.
 +
 
 +
Update 2024:  You're at the CLI on a Linux system bash shell observing files from a [[Samba]] mounted drive connected to a NAS or file server with some kind of FAT file system such as FAT32 and you wish to convert filenames and extensions with upper or mixed case to all lower case:
 +
 
 +
for f in *; do temp="${f}_temp"; mv -- "$f" "$temp"; done && for f in *_temp; do mv -- "$f" "$(echo "${f%_temp}" | tr '[:upper:]' '[:lower:]')"; done
 +
 
 +
===execute multiple BASH shell commands===
 +
{{:Execute Multiple BASH Shell Commands}}
 +
 
 +
===search for files===
 +
The two primary commands to accomplish this are
 +
#find
 +
#locate
 +
 
 +
Learn how to use [[Find and Locate]] and view examples.
  
 
== Related Pages ==
 
== Related Pages ==

Latest revision as of 13:50, 20 July 2024

Linux specific console command examples

Examples from other pages included. References also included. This document is in the form of "what you want to do" and "how to do it" taken from our old Linux forum with questions from linux users.

There are many very good and useful examples in our old Advanced Shell Operations.

change file case UPPERCASE to lowercase in the same directory

The easiest way is this simple command which uses rename:

rename 'y/A-Z/a-z/' *

Another example using tr:

for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done

Shell script example from the old Advanced Shell Operations page

for FILE in *                        # use mv -i to avoid overwriting files
do                                   # uses the tr command to convert case
  mv -i "$FILE" `echo "$FILE" | tr '[A-Z]' '[a-z]'` 2> /dev/null
done

These may fail on unsupported file systems such as FAT32 with an error such as:

mv: 'IMAG0001.JPG' and 'imag0001.jpg' are the same file
./IMAG0004.JPG not renamed: ./imag0004.jpg already exists

In this case you will have to move when you change case and move back again. Here we will change an uppercase file extension to a lowercase one under FAT32

rename 's/\.JPG$/\.jpeg/' *.JPG
rename 's/\.jpeg$/\.jpg/' *.jpeg

There are plenty of other ways to accomplish such tasks.

Update 2024: You're at the CLI on a Linux system bash shell observing files from a Samba mounted drive connected to a NAS or file server with some kind of FAT file system such as FAT32 and you wish to convert filenames and extensions with upper or mixed case to all lower case:

for f in *; do temp="${f}_temp"; mv -- "$f" "$temp"; done && for f in *_temp; do mv -- "$f" "$(echo "${f%_temp}" | tr '[:upper:]' '[:lower:]')"; done

execute multiple BASH shell commands

How to run shell commands sequentially or at the same time entered on a single line.

echo 1
sleep 5s
echo 2

(A) SEQUENTIAL: run several commands using the control operator ";" (semicolon) which will execute them sequentially. The shell will wait for each command to terminate in turn. The return status is the exit status of the last command executed.

echo 1; sleep 5s; echo 2

(B) SIMULTANEOUS: run several commands using the control operator "&" (ampersand) which will allow executing of all commands simultaneously.

echo 1 & sleep 5s & echo 2

(C) SEQUENTIAL CONDITIONAL: run several commands using double ampersands will prevent subsequent commands in the line from running of the preceding exits with exit status 1. Use the double-ampersand operator in BASH will provide conditional execution. Two commands separated by the double ampersands tells the shell to run the first command and then run the second command only if the first command succeeds with an exit status 0. It will behave like example (A) in that the shell will wait for each command to terminate, with the exception that the command must terminate successfully to continue with the next.

echo 1 && sleep 5s && echo 2

Hopefully this provides some basic clarification of how the bash shell handles multiple commands per line.

search for files

The two primary commands to accomplish this are

  1. find
  2. locate

Learn how to use Find and Locate and view examples.

Related Pages

If what you're looking for isn't on this page, try some of these related pages: