Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I was asked a question in an interview recently - How you will delete a file named '*'?
So I tried creating a file as - touch *. When I list, I don't see this file. When I edit (vi *). I opens up a file with random text.
So what is touch *?
You should look up unix "globbing".
The shell will look at the command and replace * with every file (not starting with '.' or hidden) unless you escape this special character * just or put it in single quotes '*'
You can use this to apply a command to many files without actually listing them manually.
If you want to see what * expands and how you can escape it to you can run the following commands:
echo *
echo '*'
echo \*
You can also use this with more specific pattern like
<command> a* # run the command on the list of file starting with a
If you want to see what the terminal actually executed when you ran touch * run:
echo touch *
The touch command as others have explained before will either create a empty file if the filename does not exist, or update the last modified timestamp on existing files. So you just updated the last modified timestamps of every non hidden file in the directory you executed the command in.
Note that this happens before your command is ever called. And if the list of file is VERY large (millions) you will get an error as the list of files will be too large to fit into the command buffer (aka the string of your expanded command will be too large)
Further reading : https://www.linuxjournal.com/content/bash-extended-globbing
Shells expand *. touch * is touch every nonhidden file in the current directory. If you want to create a file literally named *, you need to quote it: touch \* (or touch '*' or touch "*" and the equivalent for other commands such as rm).
According to the man touch, touch changes a timestamp of a file. If the file doesn't exist, it creates it empty, unless -c or -h parameters are given.
In Bash, * is a globbing character which expands to every non-hidden file and directory in the current directory. That means touch * will change the timestamp of every file and directory in the current directory, if there is any, to the current date and time.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to scripting. While looking at an ex-employee's R code there's a line where they call to the command line. The line/purpose is not something I know how to search for online. Any help is appreciated.
The line of code in question:
/folder1/folder2/folder3 -s file_1_name -n file_1_name -e file_2_name > file_1_name.log 2>&1
Things to note:
The syntax is bash (or derived from bash, 2>&1), though when I use the command line to check what shell is used it says tcsh (example redirect >&, no numbers).\
File names (above) are just the name, not the extension. Example: a file named "ex.sch" then file_1_name would be "ex". The only extension in the line of code is for the log file that is made.
The files are .sch files. According to this site these are for schematics, though I highly doubt that that's what they are.
The line/purpose is not something I know how to search for online. [...]
The line of code in question:
/folder1/folder2/folder3 -s file_1_name -n file_1_name -e file_2_name > file_1_name.log 2>&1
Interpreted as a Bash command line, that is executing the program or script /folder1/folder2/folder3, passing it the six command-line arguments -s file_1_name -n file_1_name -e file_2_name. It is furthermore directing the program's standard output and standard error to a file named file_1_name.log in the working directory, creating that file if it does not already exist and replacing its contents if it does. If the command works then folder3 must in fact be a regular file or a symbolic link to one, not a directory / folder.
We cannot tell you more. The significance of the command-line arguments and the behavior of the program in general are not conveyed by the name you provided. In particular, the fact that some of the arguments correspond to file names you recognize with their suffixes removed is probably meaningful, but we cannot tell you the meaning.
Additionally, you observe that
when I use the command line to check what shell is used it says tcsh
Undoubtedly you have checked what your own default shell is. That is not indicative of what shell R will use to run the command.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
What distinguishes between these two lines in the terminal?
Ms-MacBook-Pro:/ John$
and
Ms-MacBook-Pro:~ John$
/ is the root of the filesystem. So your first prompt means you are currently in the root of the filesystem and your name is John:
pwd
/
ls
usr var lib bin home etc ...
~ is your home directory (typically /home/John). So the second prompt means you are currently in your home directory and your name is John.
pwd
/home/John
ls
Downloads Desktop Documents Pictures ...
If there's a user named, say, "tomas" on your system, then ~tomas means his home directory, typically /home/tomas.
The meaning of anything that appears in your shell prompt depends on the value and interpretation of the $PS1 variable.
Type
echo "$PS1"
to see the string from which your displayed prompt is derived, and read the Bash manual (assuming your login shell is bash) to see how it's interpreted.
To see the bash manual, type info bash or go here. The interpretation of $PS1 is documented in the "Controlling the prompt" section under "Bash features" (section 6.9 in the latest version).
Most likely you have either \w or \W in your $PS1. \w expands to
The current working directory, with $HOME abbreviated with a tilde
(uses the $PROMPT_DIRTRIM variable).
and \W expands to
The basename of $PWD, with $HOME abbreviated with a tilde.
The basename is the last component of the path, so for example /foo/bar would be displayed as /foo/bar with \w, or just as bar with \W.
In your case, the first prompt:
Ms-MacBook-Pro:/ John$
probably means you're currently in the root directory, and the second:
Ms-MacBook-Pro:~ John$
means you're in your home directory.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have a git repo in a folder that is inside multiple other folders. When I want to use the command line for git, I have to do cd /Desktop/.../.../.../.../repo_folder to do git commands. Is their any way that I can set a shortcut to a folder or get to a folder faster because having to type in a 70 character long path is not ideal
Thanks in advanced!
You can use the cdable_vars option of bash that allows you to call cd with a variable name. If the argument passed to cd is not a directory, then it is assumed to be a variable name and the value of the variable is used as the destination directory.
Example of use: if you put this in your ~/.bashrc:
alias show='cat ~/.dirs'
save () {
here=`pwd`
if (( $# == 0 )); then
name=`basename $here`
elif (( $# > 1 )); then
echo "usage: save [<name>]"
return -1
else
name=$1
fi
sed -i -e "/^$name=/d" ~/.dirs
echo "$name=\"$here\"" >> ~/.dirs
source ~/.dirs
}
source ~/.dirs
shopt -s cdable_vars
Then, when your current directory is one that you want to remember, just type:
save my_dir
and the next time you want to go there, just type:
cd my_dir
As long there is no my_dir directory where you type it, it will bring you where you want. The save argument is optional. If you do not provide it the defined short hand will be the base name of the current directory:
cd /Desktop/../../../../repo_folder
save
will define repo_folder as the short hand for this directory.
The ~/.dirs file contains your variable definitions for your favourite directories. You can edit it by hand, if you wish. These definitions are evaluated every time you launch a new bash shell. Beware they may overwrite others that you also need. If it is a problem, I advice you to chose unique short hands (my_dir_repo_folder instead of repo_folder). And remember the second pitfall, when you type:
cd foo
you can go either to the local sub-directory foo if there is one or to the directory for which you defined the foo short hand. And there is a third one: if you redefine a short hand, the previous one is overwritten. So, this trick is handy but somehow dangerous because when you cd you do not know any more if you are really where you want. Customizing your prompt to show the current path may be a good idea.
The show alias is just a way to list all currently defined short hands.
Just set "start in" param in your bash shortcut properties and every time you run bash it will open your repo folder :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
In some code i found command like follows :
ls | grep Directory 1.txt
but above is not working for me
can any one suggest proper command?
The command you are looking for is find. It will traverse an arbitrarily complex directory structure and report back all files which match the criteria you specify.
find . -name 1.txt -exec dirname {} \;
This runs the dirname command on each matching file, which prints just the directory name.
If you only want to go one directory deep, GNU find has a -maxdepth option, or you can just use the shell's wildcard matching facilities.
dirname */1.txt
This assumes the file exists directly in a subdirectory of the current directory, and will only work if there is exactly one wildcard match (dirname only works on one file at a time) and the various workarounds are not a lot more elegant than just using find.
If, on the other hand, you know that 1.txt exists in the current directory, the command pwd will print out the directory name.
The command grep Directory 1.txt will open the file 1.txt in the current directory, and print out any lines matching the text Directory. It will simply ignore the standard input you are feeding it from the ls command, which is just as well, because ls does not print "Directory" anywhere useful. Also, parsing ls output is very complex, so you should probably avoid trying.
From the Ubuntu forums, you could use
dirname `find . -name someFile.txt`
Alternatively,
dirname /some/path/to/a/file.txt
Or you could strip off the last bit of the path grep returns
$ F='~/Documents/file.txt'
$ echo $(dirname "$F")
~/Documents
$ echo ${F%/*}
~/Documents
Given (just) an arbitrary relative pathname like 1.txt, it is impossible to get the directory name.
Why? Because there could be lots of 1.txt files in different directories! And (presumably) you'd have no way of knowing which was the right one.
If you have a way to disambiguate potential multiple answers, then you could use the find command to search for the file, and the apply dirname to the resulting pathname. Refer to the respective manual entries, etcetera. (But beware that find will typically search lots of directories, and that is liable to be *expensive".)
If you know that 1.txt is in the "current directory", then what you are actually asking for is the pathname of the current directory. You can get that using the pwd command.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an input file and a script running in unix bash.
the problem is every time i edit the input file in vi , the script takes the input file as it was inputed the first time.
How can i fix this ?
run
cat inputFile
to make sure it looks correct before passing it to your script. Try doing :wq! To make sure it will save the file even if the read only perms are set on the file. The "!" after wq will force a write despite permissions on the file.
Try typing ls -ltr inputFile and check the perms. If they look like below this then run chmod a+w inputFile
-r-r-r--
Use :w in vi to save your input file before executing the script.
Pure speculation, since many details are missing, but if your script opens the file and keeps it open, it will not see updates. If there is only one (hard) link to the file, then vi (assuming vi is actually vim, although I suspect most editors behave this way) will create a new file and change the link to it, but the script still has the original file open. A simple technique that might work is to create a second link to the file before you run the script:
$ ln input-file foo # Create a second link
$ script input-file # Run the script
$ vi input-file # Edit the file
This causes vim to modify its behavior so that it actually updates the file rather than creating a new one.
#user2613272: its either that you have not saved the file before executing it or you are executing some other file with similar name.
as suggested by #bjackfly, i guess you first "cat" your file before execution.