LINUX - Move hidden files - Secrets of Bash

COPY OF SITE - I have an OCD thing about thinking peoples sites might be down one day, so I copy paste the material out, but I still give full credit where its due, in this case its all due to the owner of the site: INSERT-URL-HERE - In no way am I claiming this as my own information, this is just a reference to a good site that I wanted to share, plus it probably increase that sites hits - If this is something bad and you dont want me to include this, let me know my contact information is all over this blog. opps@kossboss.com will get to me. All of the copys are listed in the Sources.

SHELL OPTIONS SPECIFICALLY TO MOVE FILES INCLUDING HIDDEN FILES
####################################################################

Sources:
http://superuser.com/questions/62141/linux-how-to-move-all-files-from-current-directory-to-upper-directory
http://askubuntu.com/questions/91740/how-to-move-all-files-in-current-folder-to-subfolder
http://unix.stackexchange.com/questions/6393/how-do-you-move-all-files-including-hidden-in-a-directory-to-another
http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
http://www.ss64.com/bash/shopt.html
http://www.thegeekstuff.com/2010/08/bash-shell-builtin-commands/

shopt -s <option> to set (enable) options
shopt -u <option>  to unset (disable) an option
shopt <option>  to see options

SIDE NOTE: All shopts below

An important shopt for this:
man bash:
dotglob If set, bash includes filenames beginning with a '.' in the results of pathname expansion.

MV ALL FILES INCLUDING HIDDENS FROM FOO TO BAR
================================================
Ex:
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/


MOVING ALL FILES FROM CURRENT FOLDER TO SUBFOLDER:
===================================================
shopt -s extglob dotglob
mv !(new) new
shopt -u dotglob

extglob so that !(new) doesnt grab new and moves it to new

and dotglob so it grabs all files including hidden files


MOVE ALL FILES INCLUDING HIDDEN FILES TO ANOTHER PLACE
shopt -s dotglob
mv * dest
shopt -u dotglob

HOW TO MOVE ALL FILES FROM CURRENT DIRECTORY TO UPPER DIRECTORY:
===============================================================

The command you are looking for is

# mv * .[^.]* ..
or (see below for more info):

# (shopt -s dotglob; mv -- * ..)
Explanation: the mv command moves files and directories. The last argument to mv is the target (in this case the directory one step "up" in the tree, ..). The arguments before that are the source files and directories. The asterisk (*) is a wildcard which matches all files which do not start with a dot. Files that start with a dot (dotfiles) are "hidden". They are matched using the pattern .[^.]* (see edit below).

See the manpage which I linked for more information on mv.

Why .[^.]* instead of .* ?

As Chris Johnsen correctly points out: the pattern .* also matches . and ... Since you don't want to (and cannot) move those, it's better to use a pattern which matches any filename starting with a dot except those two. The pattern .[^.]* does just that: it matches any filename (1) starting with a dot (2) followed by a character which is not a dot (3) followed by zero or more arbitrary characters.

As Paggas points out, we'd also have to add the pattern .??* in order to match files starting with two dots. See his answer for an alternative solution using find.

Arjan's answer mentions shopt in order to avoid all those issues with dotfiles. But then there is still the problem with files starting with a dash. And it requires three commands. Still, I like the idea. I propose to use it like this:

# (shopt -s dotglob; mv -- * ..)

This executes shopt in a subshell (thus no second call to shopt required) and uses -- so that files starting with a dash will not be interpreted as arguments to mv.

ANOTHER WAY:
============

Short answer: use

find . -mindepth 1 -maxdepth 1 -exec mv -t.. -- {} +
Long answer:

The command

mv * .* ..
will not work since .* can match . and ... But the command

mv * .[^.]* ..
will also not work, since .[^.]* won't match, e.g., ..filename! Instead, what I do is

mv * .[^.] .??* ..
which will match everything except . and ... * will match everything that doesn't start with a ., .[^.] will match all 2 character filenames starting with a dot except .., and .??* will match all filenames starting with a dot with at least 3 characters.

Better yet, you can use

find . -mindepth 1 -maxdepth 1 -exec mv -t.. -- {} +
which avoids the ugly glob hacks in mv * .[^.] .??* ..!


How do you move all files (including hidden) in a directory to another?
=========================================================================

Zsh
----
mv Foo/*(DN) Bar/
or

setopt -s glob_dots
mv Foo/*(N) Bar/
(Leave out the (N) if you know the directory is not empty.)

Bash
-----
shopt -s dotglob nullglob
mv Foo/* Bar/

Ksh93
------
If you know the directory is not empty:

FIGNORE='.?(.)'
mv Foo/* Bar/

Standard (POSIX) sh
--------------------
for x in Foo/* Foo/.[!.]* Foo/..?*; do
  if [ -e "$x" ]; then mv -- "$x" Bar/
done
If you're willing to let the mv command return an error status even though it succeeded, it's a lot simpler:

mv Foo/* Foo/.[!.]* Foo/..?* Bar/

GNU find and GNU mv
----------------------

find Foo/ -mindepth 1 -maxdepth 1 -exec mv -t Bar/ -- {} +

Standard find
--------------
If you don't mind changing to the source directory:

cd Foo/ &&
find . -name . -o -exec sh -c 'mv "$@" "$0"' ../Bar/ {} + -type d -prune

=======================================

Here's more detail about matching controlling whether dot files are matched in bash, ksh93 and zsh.

Bash
------
Set the dotglob option.

$ echo *
none zero
$ shopt -s dotglob
$ echo *
..two .one none zero
There's also the more flexible GLOBIGNORE variable, which you can set to a colon-separated list of wildcard patterns to ignore. If unset (the default setting), the shell behaves as if the value was empty if dotglob is set, and as if the value was .* if the option is set. See Filename Expansion in the manual. The pervasive directories . and .. are always omitted, unless the . is matched explicitly by the pattern.

$ GLOBIGNORE='n*'
$ echo *
..two .one zero
$ echo .*
.. ..two .one
$ unset GLOBIGNORE
$ echo .*
.. ..two .one
$ GLOBIGNORE=.:..
$ echo .*
..two .one

Ksh93
------
Set the FIGNORE variable. If unset (the default setting), the shell behaves as if the value was .*. To ignore . and .., they must be matched explicitly (the manual in ksh 93s+ 2008-01-31 states that . and .. are always ignored, but this does not match the actual behavior).

$ echo *
none zero
$ FIGNORE='.|..'
$ echo *
..two .one none zero
$ FIGNORE='n*'
$ echo *
. .. ..two .one zero
You can include dot files in a pattern by matching them explicitly.

$ unset FIGNORE
$ echo @(*|.[^.]*|..?*)
..two .one none zero
To have the expansion come out empty if the directory is empty, use the N pattern matching option: ~(N)@(*|.[^.]*|..?*) or ~(N:*|.[^.]*|..?*).

Zsh
----
Set the dot_glob option.

% echo *
none zero
% setopt dot_glob
% echo *
..two .one none zero
. and .. are never matched, even if the pattern matches the leading . explicitly.

% echo .*
..two .one
You can include dot files in a specific pattern with the D glob qualifier.

% echo *(D)
..two .one none zero
Add the N glob qualifier to make the expansion come out empty in an empty directory: *(DN).

15 USEFUL BASH SHELL BUILT-IN COMMANDS
########################################

15 Useful Bash Shell Built-in Commands (With Examples)
by SASIKALA on AUGUST 3, 2010
 

Bash has several commands that comes with the shell (i.e built inside the bash shell).

When you execute a built-in command, bash shell executes it immediately, without invoking any other program.

Bash shell built-in commands are faster than external commands, because external commands usually fork a process to execute it.

In this article let us review some useful bash shell builtins with examples.

1. Bash Export Command Example

export command is used to export a variable or function to the environment of all the child processes running in the current shell.

export varname=value
export -f functionname # exports a function in the current shell.
It exports a variable or function with a value. “env” command lists all the environment variables. In the following example, you can see that env displays the exported variable.

$ export country=India

$ env
SESSIONNAME=Console
country=India
_=/usr/bin/env
“export -p” command also displays all the exported variable in the current shell.


 
2. Bash eval Command Example

eval command combines all the given arguments and evaluates the combined expression and executes it, and returns the exit status of the executed command.

$ cat evalex.sh
if [ ! -z $1 ]
then
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | grep $1"
else
proccomm="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu"
fi
eval $procomm
The above code snippet accepts an argument, which is the pattern for a grep command. This lists the processes in the order of cpu usage and greps for a particular pattern given in the command line.

Note: This article is part of our on-going Bash Tutorial Series.

3. Bash hash Command Example

hash command maintains a hash table, which has the used command’s path names. When you execute a command, it searches for a command in the variable $PATH.
But if the command is available in the hash table, it picks up from there and executes it. Hash table maintains the number of hits encountered for each commands used so far in that shell.

$ hash
hits    command
   1    /usr/bin/cat
   2    /usr/bin/ps
   4    /usr/bin/ls
You can delete a particular command from a hash table using -d option, and -r option to reset the complete hash table.

$ hash -d cat
$ hash
hits    command
   2    /usr/bin/ps
   4    /usr/bin/ls
4. Bash pwd Command Example

pwd is a shell built-in command to print the current working directory. It basically returns the value of built in variable ${PWD}

$pwd
/home/sasikala/bash/exercises

$echo $PWD
/home/sasikala/bash/exercises
5. Bash readonly Command Example

readonly command is used to mark a variable or function as read-only, which can not be changed further.

$ cat readonly_ex.sh
#!/bin/bash
# Restricting an array as a readonly
readonly -a shells=("ksh" "bash" "sh" "csh" );
echo ${#shells[@]}

# Trying to  modify an array, it throws an error
shells[0]="gnu-bash"

echo ${shells[@]}

$ ./readonly_ex.sh
4
readonly_ex.sh: line 9: shells: readonly variable
6. Bash shift Command Example

shift command is used to shift the positional parameters left by N number of times and renames the variable accordingly after shifting.

$ cat shift.sh
#! /bin/bash

while [ $# -gt 0 ]
do
        case "$1" in

        -l) echo "List command"
            ls
       shift
            ;;
-p) echo "Process command"
       ps -a
       shift
       ;;
-t) echo "Hash Table command"
       hash
       shift
       ;;
-h) echo "Help command"
       help
       shift
   ;;
esac
done

$./shift.sh -l -t
List command analysis  break  testing t1.sh temp Hash Table command
hits    command
   1    /usr/bin/ls
7. Bash test Command Example

test command evaluates the conditional expression and returns zero or one based on the evaluation. Refer the manual page of bash, for more test operators.

#! /bin/bash

if test -z $1
then
        echo "The positional parameter \$1 is empty"
fi
8. Bash getopts Command Example

getopts command is used to parse the given command line arguments. We can define the rules for options i.e which option accepts arguments and which does not. In getopts command, if an option is followed by a colon, then it expects an argument for that option.

getopts provides two variables $OPTIND and $OPTARG which has index of the next parameter and option arguments respectively.

$ cat options.sh
#! /bin/bash

while getopts :h:r:l: OPTION
do
         case $OPTION in
          h) echo "help of $OPTARG"
             help "$OPTARG"
             ;;
          r) echo "Going to remove a file $OPTARG"
             rm -f "$OPTARG"
            ;;
         esac
done

$ ./options.sh -h jobs
help of jobs
jobs: jobs [-lnprs] [jobspec ...] or jobs -x command [args]
    Lists the active jobs.  The -l option lists process id's in addition
    to the normal information; the -p option lists process id's only.
9. Bash logout Command

Logout built in used to exit a current shell.

10. Bash umask Command Example

umask command sets a file mode creation mask for a current process. When an user creates a file, its default permission is based on the value set in umask. Default permission for file is 666, and it will be masked with the umask bits when user creates a file.

For more details please refer our article File and Directory permissions.

When user creates a file 666 is masked with 022, so default file permission would be 644.

$ umask
0022

$ > temporary

$ ls -l temporary
-rw-r--r-- 1 root root 4 Jul 26 07:48 temporary
11. Bash set Command Examples

set is a shell built-in command, which is used to set and modify the internal variables of the shell. set command without argument lists all the variables and it’s values. set command is also used to set the values for the positional parameters.

$ set +o history # To disable the history storing.
+o disables the given options.

$ set -o history
-o enables the history

$ cat set.sh
var="Welcome to thegeekstuff"
set -- $var
echo "\$1=" $1
echo "\$2=" $2
echo "\$3=" $3

$ ./set.sh
$1=Welcome
$2=to
$3=thegeekstuff
12. Bash unset Command Examples

unset built-in is used to set the shell variable to null. unset also used to delete an element of an array and
to delete complete array.

For more details on Bash array, refer our earlier article 15 Bash Array Operations

$ cat unset.sh
#!/bin/bash
#Assign values and print it
var="welcome to thegeekstuff"
echo $var

#unset the variable
unset var
echo $var

$ ./unset.sh
welcome to thegeekstuff
In the above example, after unset the variable “var” will be assigned with null string.

13. Bash let Command Example

let commands is used to perform arithmetic operations on shell variables.

$ cat arith.sh
#! /bin/bash

let arg1=12
let arg2=11

let add=$arg1+$arg2
let sub=$arg1-$arg2
let mul=$arg1*$arg2
let div=$arg1/$arg2
echo $add $sub $mul $div

$ ./arith.sh
23 1 132 1
14. Bash shopt Command Example

shopt built in command is used to set and unset a shell options. Using this command, you can make use of shell intelligence.

$cat shopt.sh
#! /bin/bash

## Before enabling xpg_echo
echo "WELCOME\n"
echo "GEEKSTUF\n"
shopt -s  xpg_echo
## After enabling xpg_echo
echo "WELCOME\n"
echo "GEEKSTUF\n"

# Before disabling aliases
alias l.='ls -l .'
l.

# After disabling aliases
shopt -u expand_aliases
l.

$ ./shopt.sh
WELCOME\n
GEEKSTUF\n
WELCOME

GEEKSTUF

total 3300
-rw------- 1 root root    1112 Jan 23  2009 anaconda-ks.cfg
-r-xr-xr-x 1 root root 3252304 Jul  1 08:25 backup
drwxr-xr-x 2 root root    4096 Jan 26  2009 Desktop
shopt.sh: line 17: l.: command not found
Before enabling xpg_echo option, the echo statement didn’t expand escape sequences. “l.” is aliased to ls -l of current directory. After disabling expand_aliases option in shell, it didn’t expand aliases, you could notice an error l. command not found.

15. Bash printf Command Example

Similar to printf in C language, bash printf built-in is used to format print operations.

In example 13, the script does arithmetic operation on two inputs. In that script instead of echo statement, you can use printf statement to print formatted output as shown below.

In arith.sh, replace the echo statement with this printf statement.

printf "Addition=%d\nSubtraction=%d\nMultiplication=%d\nDivision=%f\n" $add $sub $mul $div

$ ./arith.sh
Addition=23
Subtraction=1
Multiplication=132
Division=1.000000

ALL BASH SHOPT
################

4.3.2 The Shopt Builtin

This builtin allows you to change additional shell optional behavior.

shopt
 shopt [-pqsu] [-o] [optname …]
Toggle the values of variables controlling optional shell behavior. With no options, or with the -p option, a list of all settable options is displayed, with an indication of whether or not each is set. The -p option causes output to be displayed in a form that may be reused as input. Other options have the following meanings:

-s
Enable (set) each optname.

-u
Disable (unset) each optname.

-q
Suppresses normal output; the return status indicates whether the optname is set or unset. If multiple optname arguments are given with -q, the return status is zero if all optnames are enabled; non-zero otherwise.

-o
Restricts the values of optname to be those defined for the -o option to the set builtin (see The Set Builtin).

If either -s or -u is used with no optname arguments, the display is limited to those options which are set or unset, respectively.

Unless otherwise noted, the shopt options are disabled (off) by default.

The return status when listing options is zero if all optnames are enabled, non-zero otherwise. When setting or unsetting options, the return status is zero unless an optname is not a valid shell option.

The list of shopt options is:

autocd
If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells.

cdable_vars
If this is set, an argument to the cd builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

cdspell
If set, minor errors in the spelling of a directory component in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and a character too many. If a correction is found, the corrected path is printed, and the command proceeds. This option is only used by interactive shells.

checkhash
If this is set, Bash checks that a command found in the hash table exists before trying to execute it. If a hashed command no longer exists, a normal path search is performed.

checkjobs
If set, Bash lists the status of any stopped and running jobs before exiting an interactive shell. If any jobs are running, this causes the exit to be deferred until a second exit is attempted without an intervening command (see Job Control). The shell always postpones exiting if any jobs are stopped.

checkwinsize
If set, Bash checks the window size after each command and, if necessary, updates the values of LINES and COLUMNS.

cmdhist
If set, Bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands.

compat31
If set, Bash changes its behavior to that of version 3.1 with respect to quoted arguments to the conditional command’s ‘=~’ operator.

compat32
If set, Bash changes its behavior to that of version 3.2 with respect to locale-specific string comparison when using the ‘[[’ conditional command’s ‘<’ and ‘>’ operators. Bash versions prior to bash-4.0 use ASCII collation and strcmp(3); bash-4.1 and later use the current locale’s collation sequence and strcoll(3).

compat40
If set, Bash changes its behavior to that of version 4.0 with respect to locale-specific string comparison when using the ‘[[’ conditional command’s ‘<’ and ‘>’ operators (see previous item) and the effect of interrupting a command list.

compat41
If set, Bash, when in posix mode, treats a single quote in a double-quoted parameter expansion as a special character. The single quotes must match (an even number) and the characters between the single quotes are considered quoted. This is the behavior of POSIX mode through version 4.1. The default Bash behavior remains as in previous versions.

dirspell
If set, Bash attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist.

dotglob
If set, Bash includes filenames beginning with a ‘.’ in the results of filename expansion.

execfail
If this is set, a non-interactive shell will not exit if it cannot execute the file specified as an argument to the exec builtin command. An interactive shell does not exit if exec fails.

expand_aliases
If set, aliases are expanded as described below under Aliases, Aliases. This option is enabled by default for interactive shells.

extdebug
If set, behavior intended for use by debuggers is enabled:

The -F option to the declare builtin (see Bash Builtins) displays the source file name and line number corresponding to each function name supplied as an argument.
If the command run by the DEBUG trap returns a non-zero value, the next command is skipped and not executed.
If the command run by the DEBUG trap returns a value of 2, and the shell is executing in a subroutine (a shell function or a shell script executed by the . or source builtins), a call to return is simulated.
BASH_ARGC and BASH_ARGV are updated as described in their descriptions (see Bash Variables).
Function tracing is enabled: command substitution, shell functions, and subshells invoked with ( command ) inherit the DEBUG and RETURN traps.
Error tracing is enabled: command substitution, shell functions, and subshells invoked with ( command ) inherit the ERR trap.
extglob
If set, the extended pattern matching features described above (see Pattern Matching) are enabled.

extquote
If set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in double quotes. This option is enabled by default.

failglob
If set, patterns which fail to match filenames during filename expansion result in an expansion error.

force_fignore
If set, the suffixes specified by the FIGNORE shell variable cause words to be ignored when performing word completion even if the ignored words are the only possible completions. See Bash Variables, for a description of FIGNORE. This option is enabled by default.

globstar
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

gnu_errfmt
If set, shell error messages are written in the standard GNU error message format.

histappend
If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.

histreedit
If set, and Readline is being used, a user is given the opportunity to re-edit a failed history substitution.

histverify
If set, and Readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the Readline editing buffer, allowing further modification.

hostcomplete
If set, and Readline is being used, Bash will attempt to perform hostname completion when a word containing a ‘@’ is being completed (see Commands For Completion). This option is enabled by default.

huponexit
If set, Bash will send SIGHUP to all jobs when an interactive login shell exits (see Signals).

interactive_comments
Allow a word beginning with ‘#’ to cause that word and all remaining characters on that line to be ignored in an interactive shell. This option is enabled by default.

lastpipe
If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.

lithist
If enabled, and the cmdhist option is enabled, multi-line commands are saved to the history with embedded newlines rather than using semicolon separators where possible.

login_shell
The shell sets this option if it is started as a login shell (see Invoking Bash). The value may not be changed.

mailwarn
If set, and a file that Bash is checking for mail has been accessed since the last time it was checked, the message "The mail in mailfile has been read" is displayed.

no_empty_cmd_completion
If set, and Readline is being used, Bash will not attempt to search the PATH for possible completions when completion is attempted on an empty line.

nocaseglob
If set, Bash matches filenames in a case-insensitive fashion when performing filename expansion.

nocasematch
If set, Bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.

nullglob
If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.

progcomp
If set, the programmable completion facilities (see Programmable Completion) are enabled. This option is enabled by default.

promptvars
If set, prompt strings undergo parameter expansion, command substitution, arithmetic expansion, and quote removal after being expanded as described below (see Printing a Prompt). This option is enabled by default.

restricted_shell
The shell sets this option if it is started in restricted mode (see The Restricted Shell). The value may not be changed. This is not reset when the startup files are executed, allowing the startup files to discover whether or not a shell is restricted.

shift_verbose
If this is set, the shift builtin prints an error message when the shift count exceeds the number of positional parameters.

sourcepath
If set, the source builtin uses the value of PATH to find the directory containing the file supplied as an argument. This option is enabled by default.

xpg_echo
If set, the echo builtin expands backslash-escape sequences by default.

The return status when listing options is zero if all optnames are enabled, non-zero otherwise. When setting or unsetting options, the return status is zero unless an optname is not a valid shell option.


SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
Comments