command-line,terminal,quotes,double-quotes,cp , When to use quotes in command line? cp versus find


When to use quotes in command line? cp versus find

Question:

Tag: command-line,terminal,quotes,double-quotes,cp

I'm wondering if there is some pattern or trick to remember when or when not to use quotes in command line arguments.

e.g. what is the difference between:

find -type f -name "*<extension-with-quotes>"

and

cp <extension-without-quotes> ../<new-folder>

One needs quotes and one does not, else it gives an error. Why?


Answer:

You need quotes if you don't want the shell expanding the arguments, but instead want the argument passed through verbatim to whatever program you're trying to run. See, for example, the following program:

#include <stdio.h>
int main (int argc, char *argv[]) {
    printf ("Argument count = %d\n", argc);
    for (int i = 0; i < argc; i++)
        printf ("   %2d: [%s]\n", i, argv[i]);
    return 0;
}

which outputs its argument count and arguments. The following transcript shows how it runs with and without quotes:

$ ./testprog "*.sh"
Argument count = 2
    0: [./testprog]
    1: [*.sh]

$ ./testprog *.sh
Argument count = 7
    0: [./testprog]
    1: [xmit.sh]
    2: [gen.sh]
    3: [morph.sh]
    4: [prog.sh]
    5: [mon.sh]
    6: [test.sh]

So, for example, if you're in a directory with three log files, the shell will change your:

ls *.log

into:

ls a.log b.log c.log

before handing that list on to the ls program (the ls program will never see the *.log at all).

However, find expects a file pattern rather than a list of files, so it will want the *.log passed through as is, one single argument rather than three individual arguments expanded by the shell.

In fact, if you had only a.log in the current directory, an unquoted *.log would only find files called a.log regardless of how many other log files existed in the directories below. That's because find never saw the *.log, only the a.log that the shell expanded it to.


A similar example is with expr. If you want to know what three times seven is, you don't want to be doing:

expr 3 * 7

since the shell will first expand * into all the files in the current directory:

3 dallas_buyers_club.avi nsa_agent_list.txt whitehouse_bomb.odt 7

and expr won't be able to make much sense of that1. The correct way of doing it is along the lines of:

expr 3 '*' 7

in effect preserving the * so the program gets it unchanged.


1 Special note to the NSA, CIA, MPAA and other dark shadowy organisations formed to strike fear into the hearts of mortal men. That file list is fictional humour. I really don't want any men in dark suits showing up at my front door :-)


Related:


Set Java path in command line for only one directory


java,command-line,command-prompt
I am running a program that utilizes Scala 2.10 for work and is not compatible with Java 8, only Java 7. In a Windows 7 command line, how can I set the java path to use Java 7 ONLY for that directory?

How to compose Python exe programs using pipes?


python,python-2.7,batch-file,command-line,pipe
Python newbie here. I created a Python program, msg.py, which outputs a message (Hello, World). I created a Python program, id.py, which reads input and outputs it. Using py2exe I created an executable of each of them: msg.exe and id.exe I created a DOS batch file, set PATH to the...

AWK count number of times a term appear with respect to other columns


linux,shell,command-line,awk,sed
Given a CSV file: id, fruit, binary 1, apple, 1 2, orange, 0 3, pear, 1 4, apple, 0 5, peach, 0 6, apple, 1 How can i calculate for each unique values in fruit, the number of times the binary value =1 / number of occurences of that fruit...

How can I put a python program with multiple files in /usr/local/bin?


python,shell,command-line
I know that if my program were just one python script file, I could just start it with the shebang and put it in /usr/local/bin so that I could invoke it at any time from the command prompt. However, what if my program were multiple files, but I only want...

vbscript to run command


command-line,vbscript
I'm trying to create a script that runs the command: RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 Right now this errors out Dim objShell Set objShell = Wscript.CreateObject("WScript.Shell") sEXE = """C:\Windows\system32\RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 """ with CreateObject("WScript.Shell") .Run sEXE & " " , 1, true end with Wscript.Quit Any suggestions? Could I make the script...

Why is csc.exe crashing when I last left the output encoding as UTF8?


c#,windows,command-line,csc
I am having am having or have run into a very strange thing. I wonder if others have and why it's happening. Having run a one line program with this line System.Console.WriteLine(System.Console.OutputEncoding.EncodingName); I see the Encoding is Western European (DOS) Fine Here is a list of some codepages 1200 Unicode...

How to uninstall a program using C#? [duplicate]


c#,visual-studio,visual-studio-2012,command-line,windows-installer
This question already has an answer here: How to uninstall MSI using its Product Code in c# 4 answers I'm trying to uninstall a program using C# via Visual Studio and possibly CMD. I made several attempts but could not getting anything to go. Attempt #1: RegistryKey localMachine =...

How to set the classpath in Windows Command Line correctly


java,windows,command-line,classnotfoundexception
I have made many attempts to enter a command to run a JAR file correctly from the terminal, and I am 100% sure that all the JAR files and such are in the given paths, but I keep getting a ClassDefNotFoundException. Does it have anything to do with the way...

Importing a mysql database with command line


mysql,linux,database,command-line,phpmyadmin
Have tried all other answers on stackoverflow, and they haven't worked. I have a database in phpMyAdmin, and I need to update it. The file is too big to upload via phpMyAdmin, and I've tried increasing limits in php.ini to no avail. I think that didn't work because I updated...

How to exit if statement in bash without exiting program?


bash,terminal
Let's say I have a script which is executable. It looks like this: if true then echo "started if statement" exit fi echo "hello" When I run this script, hello is not printed, because exit exits the entire shell. Is there a way to make exit (or some other statement)...

How do I silence the HEAD of a curl request while using the silent flag?


bash,shell,curl,command-line,pipe
When I run the curl command and direct the data to a file, I get back the content of the site as expected. $ curl "www.site.com" > file.txt $ head file.txt Top of site ... However, this command shows a progress bar, which I do not want: % Total %...

How should I add a stationary progress bar to a C++ program that produces terminal output (in Linux)?


c++,linux,terminal,progress-bar
I have an existing program that contains a loop over files. It does various things, providing lots of terminal output. I want to have an overall progress bar that remains stationary on the same line at the bottom of the terminal while all of the output from the file operations...

How to release binaries on Github for different plattforms as seperate downloads?


git,github,command-line,software-distribution
I found this at github https://github.com/blog/1547-release-your-software. Their page provide a way to attach binaries to a special release. My build script compiles binaries for Windows MacOS and Linux. How to supply seperate downloads for different platforms there? AS far I can see these releases are simple git tags. Is it...

loop through different arguments in Rscript within Korn shell


r,terminal,ksh
I have an R script which I'm running in the terminal by firstly generating a .ksh file called myscript.ksh with the following information: #!/bin/ksh Rscript myscript.R 'Input1' and then run the function with ./mycode.ksh which sends the script to a node on the cluster in our department (the processes that...

If exist and errorlevels in a batch (.bat) file


windows,batch-file,command-line
I'm trying to run a delete command on the result of an SQL command but only if it returns an error code of 0. Below is my code: SqlCmd command... REM if SqlCmd command is successful run the below if exist statement if errorlevel 0 ( REM if the file...

How to list the contents of a subdirectory after finding the directory


linux,bash,terminal
This is a rather specific question, but essentially what I was wondering is if there is anyway to list the contents of a directory below the one that I search for. My case is that I have a parent directory with a ton of subdirectories that I want to search...

Gradle Assemble Debug is Successful But Does Not Work [duplicate]


android,command-line,gradle
This question already has an answer here: Why won't gradle run from within a jenkins job 2 answers After following instructions here: http://developer.android.com/tools/building/building-cmdline.html apparently you're supposed to chmod +x gradlew then ./gradlew assembleDebug which leads to the following output: /gradlew assembleDebug :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72200Library...

xargs echo colored output


terminal,xargs
I have the following command: somethingRegex | xargs -I {} sh -c 'echo -e "found \e[34m{}\e[39m";dummy {}' The color part of the echo does not work, example output: -e found \e[34mresult\e[39m dummy output repeat A plain echo does work with {} being nice blue echo -e "found \e[34m{}\e[39m" How do...

Continuous data stream from linux command line into python script


python,linux,command-line,tcp,pipe
I need to get a continuous stream of data flowing from a peripheral device, to a raspberry pi, and lastly to my computer (mac). So far, the data is can make its way continuously (in real time) from the peripheral, to the command line of my raspberry pi. Note that...

cat /dev/null to multiple files to clear existing files like logs


linux,bash,shell,command-line,tee
A good way to clear logs(syslog has a handle on file) which have frozen my linux server(out of space) I tried cat /dev/null > fileABC; cat /dev/null/ > fileXYZ How can I clear multiple files by cat /dev/null to multiple files in an efficient or single command. ...

Using the Rails console to delete odd-numbered records


ruby-on-rails-4,command-line,console
I'd like to delete records with odd numbered ID's from Hero table using the Rails console. Would someone be so kind as to suggest a way for me to do this? Kind Regards, Ryan...

command line Start.Info.Argument error


c#,command-line,wix,heat
I would like run this command with C# : "%WIX%/bin/heat.exe" dir "C:\Documents and Settings\APP" -gg -sfrag -cg Appli -out wixappli.wxs I have this code (Program.cs) : System.Diagnostics.Process process1 = new System.Diagnostics.Process(); process1.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe"); process1.StartInfo.Arguments = String.Format("/k \"%WIX%/bin/heat.exe\" dir \"{0}\" -gg -sfrag -cg Appli -out wixappli.wxs ", @"C:\Documents and...

How to get CPU utilization in % in terminal (mac)


osx,terminal,cpu
Ive seen the same question asked on linux and windows but not mac (terminal). Can anyone tell me how to get the current processor utilization in %, so an example output would be 40%. Thanks

Terminal line resetting after about 50 characters


terminal
My command line prompt will reset after around 50 characters, including the prompt. For example, if the prompt looks like: [email protected]$ and I start to type [email protected]$ blah blah blah blah blah blah I'll eventually get to the point where the command line starts to reset like so: blah blmputer$...

Via Windows command line, how can we compile a Netbeans C/C++ application?


c++,netbeans,command-line,ant,compilation
Let's take this simple C/C++ application Netbeans project folder. In Netbeans IDE, we just hit build button on the toolbar to build the application. I want to do that automatically via Windows command line, how can I do that? I did google, and found some related posts though not very...

Error in reading Ubuntu 14.04 mouse event file (/dev/input/event3) with java programmig


java,events,ubuntu,terminal,mouse
I want to handle mouse event in Linux terminal via java programming. I wrote two program via c++ and java that they do same process. when i use c++ programming to open and read file ("/dev/input/event3"-mouse event file), there is no problem while running executable file. (Ubuntu 14.04 terminal and...

How to make a scrolling menu in python-curses


python,terminal,curses,python-curses
There is a way to make a scrolling menu in python-curses? I have a list of records that I got from a query in sqlite3 and I have to show them in a box but they are more than the max number of rows: can I make a little menu...

Recursively copy /dev/null to file pattern


linux,command-line,zfs
I am using a ZFS system that has reached its disk quota. As a result, the rm command no longer is available to me. However, I am able to copy /dev/null to particular files to effectively remove them. The issue is that the directory I am trying to clear out...

Is there a way to save a mac terminal output directly to a file?


excel,osx,terminal
I am carrying out a research project for which I need to do quite a lot of calculations. I basically calculated a lot of features for structures present in a .sdf file. I automated the entire process and carried it out in the terminal of my mac. The thing is,...

Reading from linux command line with Python


python,linux,shell,command-line
Is there a way to read data that is coming into the command-line, straight into another Python script for execution?

Increment Serial Number using EXIF


windows,powershell,command-line,exif,exiftool
I am using ExifTool to change the camera body serial number to be a unique serial number for each image in a group of images numbering several hundred. The camera body serial number is being used as a second place, in addition to where the serial number for the image...

Using a command-line utility to perform the following map-updates


shell,command-line,awk,terminal
I'm a complete newbie to using command-line utilities and am wondering how to process information as following: mapping.txt: 80 001 002 81 011 012 013 014 82 021 022 ... input.txt: 81 103823044 80 103823054 81 103823064 ... Desired output.txt: 103823044|011| 103823044|012| 103823044|013| 103823044|014| 103823054|001| 103823054|002| 103823064|011| 103823064|012| 103823064|013| 103823064|014|...

Running Rscript in command line and loading packages


r,command-line,packages,rscript
I have a foo.R file which contains library("ggplot2") cat("Its working") I am trying to run foo.r via the command line using the Rscript commandRscript --default-packages=ggplot2 foo.R and it is giving me the following error: 1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no...

Eclipse CDT - No Console Output on OSX


c++,eclipse,osx,terminal,64bit
New to the C++ world and wanted to fiddle around using Eclipse's IDE for C/C++ called CDT. I am on OSX 10.10.2 using eclipse-cpp-luna-SR2-macosx-cocoa-x86_64. Sadly this simple example is not printing anything in the eclipse terminal. #include <iostream> using namespace std; int main() { cout << "Hello World" << endl;...

Mac Terminal Auto Complete


osx,terminal
I have been looking for a while for a patch for this. Usually on a Unix/Linux terminal when you press tab it will auto-complete until there are several options and then it will list the options below for you to select. For example: cd he helpFolder/ helpMe/ heIsThere/ cd help...

Why is Unix/Terminal faster than R?


r,bash,shell,unix,terminal
I'm new to Unix, however, I have recently realized that very simple Unix commands can do very simple things to large data set very very quickly. My question is why are these Unix commands so fast relative to R? Let's begin by assuming that the data is big, but not...

How do i make a condition that will end the program, and prevent further cout statements?


c++,terminal
I'm currently making a text-based game just for kicks, which gives the user three options to choose from. One of the options allows the user to continue, while the others end the game with a "GAME OVER" text. I thought that making an endGame() function would do the trick, but...

The command “sudo apt-get install python-dev, python3-dev” failed and exited with 100 during


python,python-2.7,python-3.x,command-line,pip
It seems like sudo apt-get install build-essential worked just fine but, but it gets error while installing python-pip. Here's the log of the failed build....

Is there a way to go two directories back in cygwin (linux)?


terminal,cygwin,cd
I know that you can go back one directory in cygwin by using the dash (-). But, how do you go back more than one directory in your history? I've tried cd -- it doesn't seem to work. Thanks! This would be very very helpful. ...

Why I'm getting different java versions


java,terminal,java-8
I just upgraded my Java from 1.8 update 31 to update 45. Once done, I checked in the Java Console it shows Java 8 update 45. But, when I checked in the terminal it shows java version "1.8.0_31". I checked using Verify Java Version, and it show You have the...

Get the output from executed commands through android app


java,android,linux,command-line,root
I'm performing the following code to execute linux commands in my android application that I'm creating: public void RunAsRoot(String[] cmds){ Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); } os.writeBytes("exit\n"); os.flush(); } I want to know if there is a way to...

Iterate over specific files in a directory using Bash find


bash,command-line,terminal
Shellcheck doesn't like my for over find loop in Bash. for f in $(find $src -maxdepth 1 -name '*.md'); do wc -w < "$f" >> $path/tmp.txt; done It suggests instead: 1 while IFS= read -r -d '' file 2 do 3 let count++ 4 echo "Playing file no. $count" 5...

Taking multiple header (rows matching condition) and convert into a column


bash,perl,command-line,awk,sed
Hello I have a file that has multiple Headers in it that I need to have turned into column values. The file looks like this: Day1 1,Smith,London 2,Bruce,Seattle 5,Will,Dallas Day2 1,Mike,Frisco 4,James,LA I would like the file to end up looking like this: Day1,1,Smith,London Day1,2,Bruce,Seattle Day1,5,Will,Dallas Day2,1,Mike,Frisco Day2,4,James,LA The file...

Macports switch PHP CLI version


php,bash,drupal,terminal,macports
I'm trying to switch my Terminal PHP version to 5.4 because I ran into some issues with Drush while updating my Drupal core. http://drupal.stackexchange.com/questions/112090/drush-command-errors The reason for these issues is my Terminal PHP version is different then my localhost. php -v in Terminal returns PHP 5.5.13 (cli) but my localhost...

Grails 3.0.2 missing generate-views


java,grails,command-line,ide,ggts
I'm new to grails and I'm trying to get my first HelloWorld from it. I can generate it with the controller, but I can't create a view because it isn't listed in the Grails (3.0.2) list of command. Because any IDE support grails 3.*, I can't create it by that...

How can I run the Jenkins jobs through terminal?


jenkins,terminal
I heard an version that webhooks could be used for this. Please help)

Redirect stdout using exec in subscript


linux,bash,redirect,command-line,sh
I have one main script which is started as an service. I can't modify this main script, because it is often updated. This main script starts a program, which echo any log to stdout. So i can't see any log of this program. But this main script calls at the...

running testng.xml via command line- error Cannot find class in classpath: com.companyname.SSProject.LaunchSSProject


command-line,testng
I am using following command on command line: C:\Users\MyScriptDirectory>java -classpath ".;Lib\selenium-2.45.0\libs\testng-6.8.5.jar" org.testng.TestNG testng.xml Getting error message: [TestNG] [Error] Cannot find class in classpath: com.companyname.SSProject.LaunchSSProject I have made following entry in testng.xml: <class name="com.companyname.SSProject.LaunchSSProject" /> "com.companyname.SSProject" is my package having LaunchSSProject.java file inside it When I am running the same testng.xml...